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 |
|
de4ever@gmail.com
Starting Member
36 Posts |
Posted - 2008-12-02 : 00:54:19
|
| The table is somewhat like thisCreate Table #tab1( [HistoryID] [int] IDENTITY(1,1), [IssueID] [int] NULL, [UserID] [int] NULL, [CreatedDate] [datetime] NULL, )Insert #Tab1 values(1,3,'2008-09-24 18:13:56.000')Insert #Tab1 values(1,3,'2008-09-25 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-26 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-22 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-28 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-29 18:13:56.000')select * from #Tab1will display HistoryId IssueID UserID CreatedDate1 1 3 2008-09-24 18:13:56.0002 1 3 2008-09-25 18:13:56.0003 2 4 2008-09-26 18:13:56.0004 2 4 2008-09-22 18:13:56.0005 2 4 2008-09-28 18:13:56.0006 2 4 2008-09-29 18:13:56.000i want all the rows with largest createddate group by IssueId and UserID..ie..i want one row each for each same issueid and userid with largest createdDatePlease Help... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-02 : 00:56:45
|
| [code]SELECT [HistoryID],[IssueID],[UserID],[CreatedDate]FROM(SELECT ROW_NUMBER() OVER (PARTITION BY UserID,IssueID ORDER BY CreatedDate DESC) AS Seq,*FROM #tab1)tWHERE t.Seq=1[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-02 : 01:01:03
|
just in case you're using sql 2000SELECT t.[HistoryID],t.[IssueID],t.[UserID],t.[CreatedDate] FROM #Tab1 tINNER JOIN (SELECT IssueID,UserID,MAX(CreatedDate) AS Latest FROM #Tab1 GROUP BY IssueID,UserID)t1ON t1.IssueID =t.IssueID AND t1.UserID=t.UserIDAND t1.Latest=t.CreatedDate |
 |
|
|
de4ever@gmail.com
Starting Member
36 Posts |
Posted - 2008-12-02 : 01:08:06
|
quote: Originally posted by visakh16 just in case you're using sql 2000SELECT t.[HistoryID],t.[IssueID],t.[UserID],t.[CreatedDate] FROM #Tab1 tINNER JOIN (SELECT IssueID,UserID,MAX(CreatedDate) AS Latest FROM #Tab1 GROUP BY IssueID,UserID)t1ON t1.IssueID =t.IssueID AND t1.UserID=t.UserIDAND t1.Latest=t.CreatedDate
Hi Visakh which query will be better in sqlserver2005 beacuse the result will be used for another join |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-02 : 01:17:02
|
results from profilerCreate Table #tab1([HistoryID] [int] IDENTITY(1,1),[IssueID] [int] NULL,[UserID] [int] NULL,[CreatedDate] [datetime] NULL,)Insert #Tab1 values(1,3,'2008-09-24 18:13:56.000')Insert #Tab1 values(1,3,'2008-09-25 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-26 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-22 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-28 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-29 18:13:56.000')--query 1SELECT t.[HistoryID],t.[IssueID],t.[UserID],t.[CreatedDate] FROM #Tab1 tINNER JOIN (SELECT IssueID,UserID,MAX(CreatedDate) AS Latest FROM #Tab1 GROUP BY IssueID,UserID)t1ON t1.IssueID =t.IssueID AND t1.UserID=t.UserIDAND t1.Latest=t.CreatedDate--query 2SELECT [HistoryID],[IssueID],[UserID],[CreatedDate]FROM(SELECT ROW_NUMBER() OVER (PARTITION BY UserID,IssueID ORDER BY CreatedDate DESC) AS Seq,*FROM #tab1)tWHERE t.Seq=1profiler results-----------------------------------------query cpu reads writes duration-----------------------------------------------query 1 16 168 2 69query 2 16 168 4 22 |
 |
|
|
de4ever@gmail.com
Starting Member
36 Posts |
Posted - 2008-12-02 : 01:37:38
|
quote: Originally posted by visakh16 results from profilerCreate Table #tab1([HistoryID] [int] IDENTITY(1,1),[IssueID] [int] NULL,[UserID] [int] NULL,[CreatedDate] [datetime] NULL,)Insert #Tab1 values(1,3,'2008-09-24 18:13:56.000')Insert #Tab1 values(1,3,'2008-09-25 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-26 18:13:56.000')Thanks a lot .....Insert #Tab1 values(2,4,'2008-09-22 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-28 18:13:56.000')Insert #Tab1 values(2,4,'2008-09-29 18:13:56.000')--query 1SELECT t.[HistoryID],t.[IssueID],t.[UserID],t.[CreatedDate] FROM #Tab1 tINNER JOIN (SELECT IssueID,UserID,MAX(CreatedDate) AS Latest FROM #Tab1 GROUP BY IssueID,UserID)t1ON t1.IssueID =t.IssueID AND t1.UserID=t.UserIDAND t1.Latest=t.CreatedDate--query 2SELECT [HistoryID],[IssueID],[UserID],[CreatedDate]FROM(SELECT ROW_NUMBER() OVER (PARTITION BY UserID,IssueID ORDER BY CreatedDate DESC) AS Seq,*FROM #tab1)tWHERE t.Seq=1profiler results-----------------------------------------query cpu reads writes duration-----------------------------------------------query 1 16 168 2 69query 2 16 168 4 22
|
 |
|
|
de4ever@gmail.com
Starting Member
36 Posts |
Posted - 2008-12-02 : 01:39:08
|
| Thanks a lot.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-02 : 01:54:13
|
Welcome |
 |
|
|
|
|
|
|
|