简体   繁体   中英

Select Latest 3 records

Using SQL Server 2014. I have data that lists a Unique Identifier, a Sale Data and a Sale Price. I would like to extract into a VIEW the last 3 sales for each unique Id.

Example of data:

+------+-----------+------------+-------------+
|  ID  | UNIQUE_ID |  SaleDate  | SalePrice   |
+------+-----------+------------+-------------+
| 8210 | 1-5       | 2015-09-29 |         0   |
| 8211 | 1-6       | 2016-11-01 |    485672   |
| 8212 | 1-7       | 1994-06-24 |    120000   |
| 8213 | 1-1       | 1996-09-06 |    170000   |
| 8214 | 1-1       | 2000-01-28 |    265000   |
| 8215 | 1-1       | 2013-10-02 |    305000   |
| 8216 | 1-1       | 2015-11-20 |   1425000   |
| 8217 | 1-3       | 1994-01-12 |         1   |
| 8218 | 1-3       | 2001-04-30 |         1   |
| 8219 | 1-3       | 2004-09-30 |         0   |
+------+-----------+------------+-------------+

The result in the view would list each Unique ID and then 6 fields:

  1. SaleDate1
  2. SalePrice1
  3. SaleDate2
  4. SalePrice2
  5. SaleDate3
  6. SalePrice3

Any hints appreciated.

You can use row_number() :

SELECT t.*
FROM (SELECT t.*,
             ROW_NUMBER() OVER (PARTITION BY UNIQUE_ID ORDER BY SaleDate DESC, SalePrice DESC) AS Seq
      FROM table t
     ) t
WHERE Seq <= 3;

You can use a window function to filter data and then conditional aggregation to get the 6 columns you need:

declare @tmp table(ID int, UNIQUE_ID varchar(50), SaleDate date, SalePrice int)

insert into @tmp values
     (8210, '1-5','2015-09-29', 0      )
    ,(8211, '1-6','2016-11-01', 485672 ) 
    ,(8212, '1-7','1994-06-24', 120000 ) 
    ,(8213, '1-1','1996-09-06', 170000 ) 
    ,(8214, '1-1','2000-01-28', 265000 ) 
    ,(8215, '1-1','2013-10-02', 305000 ) 
    ,(8216, '1-1','2015-11-20', 1425000)  
    ,(8217, '1-3','1994-01-12', 1      )
    ,(8218, '1-3','2001-04-30', 1      )
    ,(8219, '1-3','2004-09-30', 0      )

SELECT t.UNIQUE_ID
    ,max(case when t.Seq = 1 then SaleDate  else null end) as SaleDate1
    ,sum(case when t.Seq = 1 then SalePrice else null end) as SalePrice1
    ,max(case when t.Seq = 2 then SaleDate  else null end) as SaleDate2
    ,sum(case when t.Seq = 2 then SalePrice else null end) as SalePrice2
    ,max(case when t.Seq = 3 then SaleDate  else null end) as SaleDate3
    ,sum(case when t.Seq = 3 then SalePrice else null end) as SalePrice3
FROM (SELECT x.*,
        ROW_NUMBER() OVER (PARTITION BY UNIQUE_ID 
            ORDER BY SaleDate DESC, SalePrice DESC) AS Seq
      FROM @tmp x
     ) t
WHERE t.Seq < 4
group by t.UNIQUE_ID

Results:

在此处输入图片说明

以下查询返回每件商品最近出售的3行

select * from ( select UNIQUE_ID,SaleDate,SalePrice,rank() over (partition by UNIQUE_ID order by SaleDate desc) as rnk from salestable ) where rnk<4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM