简体   繁体   中英

MS SQL Insert most recent three dates from one table into another table matching on one column

I have a 'source' table containing a list of user logins along with the date and time of login. The 'source' table looks like this...

CREATE TABLE [dbo].[tblSrc](
[userID] [varchar](50) NULL,
[date] [datetime] NULL)

For illustration purposes some rows may contain...

userID    date
------    ----
1         2013-01-01 00:00:00.000
2         2013-01-01 00:00:00.000
3         2013-01-01 00:00:00.000
2         2013-01-02 00:00:00.000
3         2013-01-03 00:00:00.000
3         2013-01-04 00:00:00.000
1         2013-01-02 00:00:00.000
3         2013-01-05 00:00:00.000

I have a 'destination' table as follows...

CREATE TABLE [dbo].[tblDest](
[userID] [varchar](50) NOT NULL,
[date1] [datetime] NULL,
[date2] [datetime] NULL,
[date3] [datetime] NULL)

The userID in the source table contains multiple duplicate userID's whereas I've populated the destination table with unique userID's.

What I need to do is to itterate through the source table and insert the last three login dates per user?

So by way of example the destination table would like this using userID '3'

user '3' has 4 entries in the source table

userID    date1                     date2                     date3
------    -----                     -----                     -----
3         2013-01-05 00:00:00.000   2013-01-04 00:00:00.000   2013-01-03 00:00:00.000

date1 being the most recent login date, date2 then next and date3 the next.

I can get the latest date from the source table per userID but I can't figure out how to get the three most recent (assuming there are three dates) and insert them into the destination table.

Any help would be very much appreciated.

This SQL should work:

 with userrows as
(SELECT t.userID, t.date,
ROW_NUMBER() OVER(PARTITION BY t.userID ORDER BY t.date DESC) AS Row
FROM tblSrc t)
insert into tblDest
select distinct
    u.userID,
(select top 1 date from userrows u1 where u1.userID=u.userID and u1.Row=1 ) 'date1',
(select top 1 date from userrows u2 where u2.userID=u.userID and u2.Row=2)  'date2',
(select top 1 date from userrows u3 where u3.userID=u.userID  and u3.Row=3)  'date3'
from userrows u

To add status you could do something like this:

 with userrows as
(SELECT t.userID, t.date,t.status,
ROW_NUMBER() OVER(PARTITION BY t.userID ORDER BY t.date DESC) AS Row
FROM tblSrc t)
insert into tblDest
 select distinct
u.userID,
(select date from userrows u1 where u1.userID=u.userID and u1.Row=1 ) 'date1',
(select status from userrows u1 where u1.userID=u.userID and u1.Row=1 ) 'status1',
(select date from userrows u2 where u2.userID=u.userID and u2.Row=2)  'date2',
(select status from userrows u2 where u2.userID=u.userID and u2.Row=2 ) 'status2',
(select date from userrows u3 where u3.userID=u.userID  and u3.Row=3)  'date3',
(select status from userrows u3 where u3.userID=u.userID and u3.Row=3 ) 'status3'
 from userrows u

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