Question

I want to take the last request date from a union column. here is my table :

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

The expected table:

 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  |           

when the ID 001 request Item Shirt again then it will added the last request date from the last time the ID 001 request the same item. for example ID 001 request Shirt on 10/05/2014 then, it added to the new row :

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

here is the code edited based on 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 

I tried to added this:

    ;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 

but it display an error "tbl_Request.date doesn't have an aggregate....." .

does it can't use max(date)?

Thanks in advances....

Was it helpful?

Solution

Here you can generate your expected table

 ;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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top