2016-11-16 1 views
0

顧客情報のデータダンプがあるとします。(account, invoiceAmount, invoiceDate). このレコードには複数の請求書日付データが含まれています。最後の3つの請求書日付を持つ顧客の記録を取り出す必要があります。 [MAX(invoiceDate), MAX(invoiceDate)-1, MAX(invoiceDate)-2]SQL SERVERで異なるアカウントのテーブルから複数の最大値を同時に見つけ出す

お客様ごとに異なる請求書がある可能性があります。

account   invoicedate ageamount 
1-129285408641 2016-02-08 92 
1-129285650772 2016-02-08 666 
1-129285408641 2016-07-08 717 
1-129285650772 2016-07-08 885 
1-129285650772 2015-09-08 766 
1-129285408641 2016-05-08 1479 
1-129285650772 2016-05-08 637 
1-129285650772 2015-10-08 682 
1-129285408641 2016-03-08 668 
1-129285650772 2016-01-08 637 
1-129285650772 2015-08-08 439 
1-129285650772 2015-12-08 641 
1-129285650772 2015-07-08 109 
1-129285408641 2016-06-08 183 
1-129285650772 2016-06-08 914 
1-129285650772 2016-08-08 415 
1-129285408641 2016-08-08 1198 
1-129285650772 2016-10-08 579 
1-129285408641 2016-11-08 250 
1-129285650772 2016-11-08 1148 
1-129285650772 2015-11-08 694 
1-129285408641 2015-09-08 1363 
1-129285650772 2016-03-08 748 
1-129285408641 2016-01-08 1347 
1-129285408641 2015-11-08 442 
1-129285408641 2015-08-08 409 
1-129285408641 2015-12-08 918 
1-129285408641 2015-07-08 109 
1-129285408641 2016-04-08 421 
1-129285650772 2016-04-08 637 
1-129285650772 2016-09-08 1000 
1-129285408641 2016-09-08 119 
1-129285408641 2016-10-08 1228 

サンプルデータでは、これらの2つのサンプルアカウントの請求書を最後に取得する必要があります。

どのようなクエリですか?

+0

。詳細を提供 – Viki888

+0

サンプルデータと期待される結果を提供 – Mansoor

+0

@Mansoorサンプルデータをどのように共有するのですか? –

答えて

1

あなたが実際にあなたが同じまたは異なる行でこれをするかどうかを指定していないとして、ここでは両方のソリューションです:あなたの質問は明確ではない

-- Build up the test data: 
declare @t table(account nvarchar(50), invoicedate date, ageamount int); 
insert into @t values 
,('1-129285408641','2016-01-08',1347) 
,('1-129285408641','2015-11-08',442) 
,('1-129285408641','2015-08-08',409) 
,('1-129285408641','2015-12-08',918) 
,('1-129285408641','2015-07-08',109) 
,('1-129285408641','2016-04-08',421) 
,('1-129285650772','2016-04-08',637) 
,('1-129285650772','2016-09-08',1000) 
,('1-129285408641','2016-09-08',119) 
,('1-129285408641','2016-10-08',1228); 


-- To return last three invoice dates in the same row: 
with cte 
as 
(
    select account 
      ,invoicedate 
      ,row_number() over (partition by account order by invoicedate desc) as rownum 
    from @t e 
) 
select Account 
     ,[1] as MostRecentInvoiceDate 
     ,[2] as SecondMostRecentInvoiceDate 
     ,[3] as ThirdMostRecentInvoiceDate 
from 
( 
    select c.account 
      ,c.invoicedate 
      ,c.rownum 
    from cte c 
     join @t t on c.account = t.account 
    where c.rownum <= 3 
)a 
pivot 
    (
    max(invoicedate) for rownum in ([1],[2],[3]) 
    ) pvt1; 


-- To return last three invoice dates as seperate rows: 
with cte 
as 
(
    select account 
      ,invoicedate 
      ,row_number() over (partition by account order by invoicedate desc) as rownum 
    from @t e 
) 
select Account 
     ,InvoiceDate 
     ,rownum 
from cte 
where rownum <= 3 
order by account 
     ,invoicedate desc; 
+0

優秀ですが、ここで私は請求書も欲しいです。 –

関連する問題