2017-06-02 39 views
3

我需要根據最後一張發票數據爲每個用戶提取數據。這裏的數據:SQL - MAX值不符合相應的值

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

你會看到,我有與一切的一切,所以說話,但不是最大日期,反之亦然。我需要列中的每條記錄與最大日期值相關。請告訴我我做錯了什麼。

+0

您還可以提供當前和期望的輸出樣本嗎?我不太明白你想做什麼。 –

+0

@Hexadect是,按帳號分組。只有兩個不同的帳號。我得到的輸出可以在鏈接後的小提琴網站上看到。最大日期值是正確的,但金額和其他一切都與每個帳戶的最大日期所在的行無關。 –

+0

你在桌上有獨特的領域嗎?我們可以假設'id'是唯一的嗎? – gaborsch

回答

0

爲了讓剛剛從過去的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 
+0

嗨Sorack, 謝謝,它的工作原理,但我需要爲每個帳戶的每個發票上次發票金額和平均金額。現在這兩個數額都是相同的。 –

+0

@IvoVancāns這是因爲我們將發佈的結果分組,我們需要一個新的「子查詢」才能獲得總數。我將使用這個子查詢編輯我的答案 – Sorack

+0

@IvoVancāns完成 – Sorack

0

我希望代碼可以幫助你。

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