【问题标题】:SQL - MAX value doesn't fit with the appropriate valuesSQL - MAX 值不符合适当的值
【发布时间】:2017-06-02 12:20:26
【问题描述】:

我需要根据上次发票数据为每个用户提取数据。这是数据:

CREATE TABLE bite
    (`ACCOUNT` int, `ISSUEDATE` datetime, `REFERENCE` int, 
     `ID` int, `AMOUNT` decimal(10,2))
;

INSERT INTO bite
    (`ACCOUNT`, `ISSUEDATE`, `REFERENCE`, `ID`, `AMOUNT`)
VALUES
    (2947471, '2012-12-31 00:00:00', 0005632765, 11543487, 40.18),
    (2947471, '2016-12-30 00:00:00', 0017945914, 36672073, 34.75),
    (2947471, '2012-11-30 00:00:00', 0005448824, 11001690, 33.69),
    (2947471, '2013-03-31 00:00:00', 0006316596, 12759258, 33.44),
    (2956987, '2015-07-01 00:00:00', 0012754607, 26035486, 26.79),
    (2956987, '2015-12-30 00:00:00', 0014371958, 29254616, 25.14),
    (2947471, '2014-05-31 00:00:00', 0009478470, 19223669, 23.34),
    (2947471, '2013-03-02 00:00:00', 0006177725, 12352924, 21.17),
    (2956987, '2014-05-31 00:00:00', 0009504785, 19223632, 20.24),
    (2947471, '2014-01-31 00:00:00', 0008753563, 17845265, 19.97),
    (2947471, '2013-06-30 00:00:00', 0007174573, 14523560, 19.28),
    (2947471, '2014-06-30 00:00:00', 0010048671, 20171164, 19.22),
    (2947471, '2015-01-31 00:00:00', 0011793537, 23926223, 18.98),
    (2947471, '2014-03-31 00:00:00', 0009215609, 18762696, 18.26),
    (2947471, '2013-11-30 00:00:00', 0008394094, 16600223, 18.14),
    (2947471, '2013-04-30 00:00:00', 0006818798, 13568161, 18.08),
    (2956987, '2015-06-30 00:00:00', 0013061086, 26579781, 18.03),
    (2956987, '2014-10-31 00:00:00', 0010942060, 22376155, 18),
    (2947471, '2013-12-31 00:00:00', 0008440468, 17395187, 18),
    (2947471, '2013-10-31 00:00:00', 0007958174, 16156026, 17.94),
    (2947471, '2013-08-31 00:00:00', 0007585482, 15274737, 17.79),
    (2947471, '2012-11-30 00:00:00', 0005632765, 11543487, 40.18),
    (2947471, '2016-11-30 00:00:00', 0017945914, 36672073, 34.75),
    (2947471, '2012-10-31 00:00:00', 0005448824, 11001690, 33.69),
    (2947471, '2013-02-28 00:00:00', 0006316596, 12759258, 33.44),
    (2956987, '2015-05-31 00:00:00', 0012754607, 26035486, 26.79),
    (2956987, '2015-11-30 00:00:00', 0014371958, 29254616, 25.14)
;

这是查询:

select account, max(issuedate) as "LAST INVOICE DATE",
  reference,amount,round(avg(amount),2) as "AVERAGE AMOUNT" 
from bite 
group by account

(也可以从这里的小提琴中获得:http://sqlfiddle.com/#!9/5280b7/58/0

你会看到我拥有与所有事物相关的一切,可以这么说,但不是最大日期,反之亦然。我需要列中的每条记录都与最大日期值相关。请告诉我我做错了什么。

【问题讨论】:

  • 您能否提供当前和所需输出的样本?我不太明白你想要做什么。
  • @Hexadect 是,按帐号分组。只有两个不同的帐号。我得到的输出可以在以下链接的小提琴网站上看到。最大日期值是正确的,但金额和其他所有内容与每个帐户的最大日期所在的行无关。
  • 表中有唯一的字段吗?我们可以假设id 是唯一的吗?
  • @IvoVancāns 我注意到你删除了你说你使用了某人的答案的帖子。在这种情况下,正确的程序是赞成那些帮助过你的人,并接受他/她的回答。请善待那些帮助你的人。

标签: mysql sql


【解决方案1】:

希望代码可以帮助到你。

with withbite
as(
select row_number()over(partition by account order by issuedate desc) as line,
        account, 
       issuedate,
       reference,
       amount
from bite 
)
select account, 
       issuedate as 'LAST INVOICE DATE',
       reference,
       amount,
       round(avg(amount),2) as 'AVERAGE AMOUNT'
from withbite
where line=1
group by account, 
       issuedate,
       reference,
       amount

【讨论】:

    【解决方案2】:

    为了仅从最后一个 issuedate 获取数据,您需要使用 subquery 检索 max issuedate

    select account,
           max(issuedate) as "LAST INVOICE DATE",
           amount,
           (select round(avg(b3.amount),2)
              from bite b3
             where b3.account = b.account) as "AVERAGE AMOUNT" 
      from bite b
     where b.issuedate = (select max(b2.issuedate)
                            from bite b2
                           where b2.account = b.account)
     group by account, issuedate
    

    【讨论】:

    • 嘿 Sorack,谢谢,它有效,但我需要每个帐户的最后一张发票的金额和每张发票的平均金额。现在这两个金额在某种程度上是相同的。
    • @IvoVancāns 那是因为我们将结果分组为发出日期,我们需要一个新的subquery 才能获得总量。我会用这个subquery编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多