Author |
Topic |
salmonraju
Yak Posting Veteran
54 Posts |
Posted - 2007-12-06 : 16:14:36
|
hi , I have table like belowTrainID UserID MsgType User Data CreatedDate (dd/mm/yy)1111 1 SystemAinfo xtgdttggt 01/10/071112 1 SystemBinfo xtgsaadggt 04/10/071113 1 SystemCinfo xtgdsatggt 02/10/071114 2 SystemAinfo xtgdstggt 08/10/071115 2 SystemBinfo xtgdstggt 11/10/071116 2 SystemCinfo xtgdstggt 10/10/071117 1 SystemAinfo xtgdstggt 12/10/071118 1 SystemBinfo xtgdstggt 13/10/071119 2 SystemCinfo xtgdstggt 15/10/07user have 3 types of messages (SystemAinfo,SystemBinfo,SystemAinfo) for a 'particular user' a 'particular message' can appear number of times but with different createdDate (column)i want a query to retrive UserData (column) value for SystemBinfo messageType that have maximum createDate for a particular userfrom the above for userid 1 i need the below data1118 1 SystemBinfo xtgdstggt 13/10/07 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-06 : 16:26:55
|
[code]DECLARE @Sample TABLE (TrainID INT, UserID INT, MsgType VARCHAR(20), UserData VARCHAR(20), CreatedDate DATETIME)SET DATEFORMAT DMYINSERT @SampleSELECT 1111, 1, 'SystemAinfo', 'xtgdttggt', '01/10/07' UNION ALLSELECT 1112, 1, 'SystemBinfo', 'xtgsaadggt', '04/10/07' UNION ALLSELECT 1113, 1, 'SystemCinfo', 'xtgdsatggt', '02/10/07' UNION ALLSELECT 1114, 2, 'SystemAinfo', 'xtgdstggt', '08/10/07' UNION ALLSELECT 1115, 2, 'SystemBinfo', 'xtgdstggt', '11/10/07' UNION ALLSELECT 1116, 2, 'SystemCinfo', 'xtgdstggt', '10/10/07' UNION ALLSELECT 1117, 1, 'SystemAinfo', 'xtgdstggt', '12/10/07' UNION ALLSELECT 1118, 1, 'SystemBinfo', 'xtgdstggt', '13/10/07' UNION ALLSELECT 1119, 2, 'SystemCinfo', 'xtgdstggt', '15/10/07'SELECT TrainID, UserID, UserData, CreatedDateFROM ( SELECT TrainID, UserID, UserData, CreatedDate, ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY CreatedDate DESC) AS RowID FROM @Sample WHERE MsgType = 'SystemBinfo' ) AS dWHERE RowID = 1ORDER BY UserID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
salmonraju
Yak Posting Veteran
54 Posts |
Posted - 2007-12-07 : 11:32:27
|
sorry for late Response,I am getting the following error'ROW_NUMBER' is not a recognized function name. I am using sql server 2000 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 11:36:11
|
Really?You posted in the SQL Server 2005 forum... E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 11:38:37
|
[code]DECLARE @Sample TABLE (TrainID INT, UserID INT, MsgType VARCHAR(20), UserData VARCHAR(20), CreatedDate DATETIME)SET DATEFORMAT DMYINSERT @SampleSELECT 1111, 1, 'SystemAinfo', 'xtgdttggt', '01/10/07' UNION ALLSELECT 1112, 1, 'SystemBinfo', 'xtgsaadggt', '04/10/07' UNION ALLSELECT 1113, 1, 'SystemCinfo', 'xtgdsatggt', '02/10/07' UNION ALLSELECT 1114, 2, 'SystemAinfo', 'xtgdstggt', '08/10/07' UNION ALLSELECT 1115, 2, 'SystemBinfo', 'xtgdstggt', '11/10/07' UNION ALLSELECT 1116, 2, 'SystemCinfo', 'xtgdstggt', '10/10/07' UNION ALLSELECT 1117, 1, 'SystemAinfo', 'xtgdstggt', '12/10/07' UNION ALLSELECT 1118, 1, 'SystemBinfo', 'xtgdstggt', '13/10/07' UNION ALLSELECT 1119, 2, 'SystemCinfo', 'xtgdstggt', '15/10/07'SELECT s.TrainID, s.UserID, s.MsgType, s.UserData, s.CreatedDateFROM @Sample AS sINNER JOIN ( SELECT UserID, MAX(CreatedDate) AS mcd FROM @Sample WHERE MsgType = 'SystemBinfo' GROUP BY UserID ) AS p ON p.UserID = s.UserIDWHERE p.mcd = s.CreatedDate[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|