如何在SQL Server中获取最近和最近的值

[英]How to get most recent and second recent values in SQL Server


I have table containing data related to Risks and I have to get most recent and 2nd second recent data from Risk Table

我有包含与风险相关的数据的表,我必须从风险表中获得最近的和第2秒的最近的数据

Data in table is as follow:

表中数据如下:

RiskID        RiskName     RiskScore       RiskDate    ItemID      
1             ABC            10          23/10/2013       12            
1             ABC            8           20/12/2013       12 
1             ABC            5           15/01/2014       12
2             BC             9           19/09/2013       12
2             BC             10          17/12/2013       12
2             BC             9           12/01/2014       12

Where I am looking for result like below where ItemID should be 12

我正在寻找的结果如下,ItemID应该是12

R_ID   R_Name  Recent_R_Date  Recent_R_Score  Second_R_Date   Second_R_Date  ItemID
1       ABC       15/01/2014      5              20/12/2013       8           12
2       BC        12/01/2014      9             17/12/2013        10          12

2 个解决方案

#1


3  

One way to do it (which also works in SQL Server 2008)

一种方法(也适用于SQL Server 2008)

SELECT RiskID, RiskName,
       MAX(CASE WHEN rnum = 1 THEN RiskDate END) RecentDate,
       MAX(CASE WHEN rnum = 1 THEN RiskScore END) RecentScore,
       MAX(CASE WHEN rnum = 2 THEN RiskDate END) SecondDate,
       MAX(CASE WHEN rnum = 2 THEN RiskScore END) SecondScore,
       ItemID
  FROM
(
  SELECT *, ROW_NUMBER() OVER (PARTITION BY RiskID ORDER BY RiskDate DESC) rnum
    FROM Risk
   WHERE ItemID = 12
) q
 WHERE rnum <= 2
 GROUP BY ItemID, RiskID, RiskName
 ORDER BY RiskID

Output:

输出:

| RISKID | RISKNAME | RECENTDATE | RECENTSCORE | SECONDDATE | SECONDSCORE | ITEMID |
|--------|----------|------------|-------------|------------|-------------|--------|
|      1 |      ABC | 2014-01-15 |           5 | 2013-12-20 |           8 |     12 |
|      2 |       BC | 2014-01-12 |           9 | 2013-12-17 |          10 |     12 |

Here is SQLFiddle demo

这是SQLFiddle演示

#2


3  

Edit:

编辑:

Since you've tagged with sql-server-2012, you will have access to the LEAD and LAG analytic functions, which allow you to 'peek' at other rows. I've also assumed you need to 'partition by' the RiskName.

由于您已经使用sql-server-2012进行了标记,您将可以访问LEAD和LAG analytic functions,它允许您“窥视”其他行。我还假设您需要通过RiskName进行分区。

SELECT TOP 2
    RiskId, RiskName, RiskDate AS Recent_R_Date, RiskScore AS Recent_R_Score, 
    LAG(RiskDate) OVER (PARTITION BY RiskName ORDER BY RiskDate ASC) AS Second_R_Date,
    LAG(RiskScore) OVER (PARTITION BY RiskName ORDER BY RiskDate ASC) AS Second_R_Score,
    ItemID
 FROM Risk
 WHERE ItemID = 12
 ORDER BY RiskDate DESC;

Fiddle here

小提琴在这里

It should be as simple as ordering by date DESC and then limiting the result to 2, viz: SELECT TOP 2 R_ID, R_Name, Recent_R_Date, Recent_R_Score, Second_R_Date, Second_R_Date, ItemID FROM Risks WHERE ItemID = 12 ORDER BY RiskDate DESC;
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/02/05/a594fd65545ae48a8528189edcaa810e.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告