如何从一组数据中获取最后一个请求日期

时间:2014-05-14 09:17:19

标签: sql sql-server select union

我想从union列中获取最后一个请求日期。 这是我的表:

 ID   |  Items1 |  Items2   |   Date
 001  |  Shirt  |  CPU      |   24/04/2014  
 002  |  Shirt  | CPU       |   25/04/2014      
 001  |  Shoes  | Monitor   |   26/04/2014   

预期表格:

 ID   |  Items     |  Date        | Last Request Date
 001  |  Shirt     |  24/04/2014  |      
 002  |  Shirt     |  25/04/2014  |       
 001  |  Shoes     |  26/04/2014  |       
 001  |  CPU       |  24/04/2014  |      
 002  |  CPU       |  25/04/2014  |       
 001  |  Monitor   |  26/04/2014  |           

当ID 001再次请求Item Shirt时,它将添加上次ID 001请求相同项目的最后一个请求日期。 例如ID 01 request Shirt on 10/05/2014 then,它添加到新行:

 ID   |  Items   |  Request Date  |   Last Request Date     
 001  |  Shirt   |  10/05/2014    | 24/04/2014     

这是基于shree.pat18 sugestion编辑的代码:

   ;WITH cte 
     AS (SELECT [date], 
                id, 
                item1                   item, 
                Row_number() 
                  OVER ( 
                    partition BY id, item1 
                    ORDER BY Getdate()) rn 
         FROM   items 
         WHERE  item1 IS NOT NULL 
         UNION 
         SELECT [date], 
                id, 
                item2                   item, 
                Row_number() 
                  OVER ( 
                    partition BY id, item2 
                    ORDER BY Getdate()) rn 
         FROM   items 
         WHERE  item2 IS NOT NULL) 
SELECT id, 
       item, 
       date 
FROM   cte 

我试图补充一下:

    ;WITH cte 
     AS (SELECT [date], 
                Max(date)               AS [Lastdate], 
                id, 
                item1                   item, 
                Row_number() 
                  OVER ( 
                    partition BY id, item1 
                    ORDER BY Getdate()) rn 
         FROM   items 
         WHERE  item1 IS NOT NULL 
         UNION 
         SELECT [date], 
                Max(date)               AS [Lastdate], 
                id, 
                item2                   item, 
                Row_number() 
                  OVER ( 
                    partition BY id, item2 
                    ORDER BY Getdate()) rn 
         FROM   items 
         WHERE  item2 IS NOT NULL) 
SELECT id, 
       item, 
       date, 
       [lastdate] 
FROM   cte 

但它显示错误“ tbl_Request.date没有聚合..... ”。

不能使用最大(日期)吗?

谢谢你的进步......

1 个答案:

答案 0 :(得分:1)

您可以在此处生成预期的表格

 ;WITH cte 
     AS (SELECT DISTINCT T.id, 
                         Item1.* 
         FROM   #temp T 
                CROSS apply (SELECT item1, 
                                    dt 
                             FROM   #temp T1 
                             WHERE  T1.id = T.id) AS Item1 
         UNION 
         SELECT DISTINCT T.id, 
                         Item2.* 
         FROM   #temp T 
                CROSS apply (SELECT item2, 
                                    dt 
                             FROM   #temp T1 
                             WHERE  T1.id = T.id) AS Item2) 
SELECT C.*, 
       Isnull((SELECT TOP 1 dt 
               FROM   cte 
               WHERE  cte.id = c.id 
                      AND cte.dt < C.dt), C.dt) AS LastRequested 
FROM   cte C