2016-11-16 1 views
0

다른 계정 테이블에서 최대 값을 여러 개 찾으십시오. 고객 세부 정보 데이터 덤프가 있다고 가정하십시오. (account, invoiceAmount, invoiceDate). 이 레코드에는 여러 invoiceDates 데이터가 들어 있습니다. 지난 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 

샘플 데이터는 마지막 두 장의 샘플 계정 인보이스를 가져와야합니다.

무엇을 쿼리합니까? 이 동일하거나 서로 다른 행 여부 당신이 실제로 지정하지 않은 것처럼

+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

우수하지만 여기에 인보이스 발행 물도 필요합니다. –