Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ladiaocb
Yak Posting Veteran
57 Posts |
Posted - 2008-08-26 : 12:48:33
|
Need help with an update query. I have the following table:tbl1userid----monid----date-------score1---------Null-----8/1/2008---1001---------Null-----8/22/2008--981---------Null-----8/15/2008--881---------Null-----8/10/2008--902---------Null-----8/4/2008---1002---------Null-----8/25/2008--752---------Null-----8/18/2008--99I am trying to update the "monid" column to reflect the sequence number based on the date. For example:userid----monid----date-------score1---------1--------8/1/2008---1001---------4--------8/22/2008--981---------3--------8/15/2008--881---------2--------8/10/2008--902---------1--------8/4/2008---1002---------3--------8/25/2008--752---------2--------8/18/2008--99Any help is greatly appreciated! |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-08-26 : 13:16:26
|
DECLARE @TABLE TABLE (userid int,monid int,aDate datetime,Score int)INSERT INTO @tableSELECT 1,Null,'8/1/2008',100 UNION ALLSELECT 1,Null,'8/22/2008',98 UNION ALLSELECT 1,Null,'8/15/2008',88 UNION ALLSELECT 1,Null,'8/10/2008',90 UNION ALLSELECT 2,Null,'8/4/2008',100 UNION ALLSELECT 2,Null,'8/25/2008',75 UNION ALLSELECT 2,Null,'8/18/2008',99SELECT a.userid,a.adate,[MonoID] = count(*)FROM @table aINNER JOIN @table b ON a.userid = b.useridand a.aDate <= b.aDategroup by a.userid,a.adate order by 1,3 ascJim |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-08-26 : 13:22:57
|
DECLARE @TABLE TABLE (userid int,monoid int,aDate datetime,Score int)INSERT INTO @tableSELECT 1,Null,'8/1/2008',100 UNION ALLSELECT 1,Null,'8/22/2008',98 UNION ALLSELECT 1,Null,'8/15/2008',88 UNION ALLSELECT 1,Null,'8/10/2008',90 UNION ALLSELECT 2,Null,'8/4/2008',100 UNION ALLSELECT 2,Null,'8/25/2008',75 UNION ALLSELECT 2,Null,'8/18/2008',99UPDATE @TABLESET MonoID =b.MonoID FROM @table aINNER JOIN( select a.userid,a.adate,[MonoID] = count(*) from @table a inner join @table b on a.userid = b.userid and a.aDate <= b.aDate group by a.userid,a.adate ) bON a.userid = b.useridand a.aDate = b.aDateSELECT * FROM @table ORDER BY 1,2 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-26 : 13:35:20
|
quote: Originally posted by ladiaocb Need help with an update query. I have the following table:tbl1userid----monid----date-------score1---------Null-----8/1/2008---1001---------Null-----8/22/2008--981---------Null-----8/15/2008--881---------Null-----8/10/2008--902---------Null-----8/4/2008---1002---------Null-----8/25/2008--752---------Null-----8/18/2008--99I am trying to update the "monid" column to reflect the sequence number based on the date. For example:userid----monid----date-------score1---------1--------8/1/2008---1001---------4--------8/22/2008--981---------3--------8/15/2008--881---------2--------8/10/2008--902---------1--------8/4/2008---1002---------3--------8/25/2008--752---------2--------8/18/2008--99Any help is greatly appreciated!
Minor variationUPDATE tSET t.monid=tmp.SeqFROM YourTable tINNER JOIN (SELECT (SELECT COUNT(*) FROM YourTable WHERE userid=a.userid and date<a.date)+1 AS Seq, *FROM YourTable a)tmpON tmp.userid=t.useridAND tmp.date=t.date |
 |
|
|
|
|
|
|