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 |
|
yaman
Posting Yak Master
213 Posts |
Posted - 2011-05-19 : 16:56:33
|
| Hello Sir , This is my table data date ConsultantID ConsultantStatusID2011-05-20 01:39:30.840 19205 32011-05-20 01:34:10.617 19206 32011-05-20 01:34:58.547 19207 32011-05-20 04:16:22.963 19207 122011-05-20 01:33:42.543 19208 32011-05-20 04:16:22.963 19208 122011-05-20 01:33:12.283 19210 32011-05-20 04:16:22.963 19210 12I want to fetch only that record whose date is maximum according to consultantid wise .Please help me out sirYaman |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-19 : 17:01:21
|
| ;WITH a(Date, ConsultantID, ConsultantStatusID, rn) AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY ConsultantID ORDER BY Date DESC) FROM myTable)SELECT Date, ConsultantID, ConsultantStatusID FROM aWHERE rn=1 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
yaman
Posting Yak Master
213 Posts |
Posted - 2011-05-19 : 17:39:44
|
quote: Originally posted by robvolk ;WITH a(Date, ConsultantID, ConsultantStatusID, rn) AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY ConsultantID ORDER BY Date DESC) FROM myTable)SELECT Date, ConsultantID, ConsultantStatusID FROM aWHERE rn=1
Thanks Alot Sir ,I am able to fetch data but i am not able insert query data in to another table .INSERT INTO teat1(CID,ConsultantStatus,StatusComment,StatusDate) WITH a( ConsultantID, ConsultantStatusID,StatusComment, Date,rn) AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY ConsultantID ORDER BY Date DESC) FROM test)SELECT ConsultantID, ConsultantStatusID,StatusComment,Date FROM aWHERE rn=1 Please help me out Sir .Yaman |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-19 : 17:41:43
|
| WITH a( ConsultantID, ConsultantStatusID,StatusComment, Date,rn) AS (INSERT INTO teat1(CID,ConsultantStatus,StatusComment,StatusDate) SELECT *, ROW_NUMBER() OVER (PARTITION BY ConsultantID ORDER BY Date DESC) FROM test)SELECT ConsultantID, ConsultantStatusID,StatusComment,Date FROM aWHERE rn=1 |
 |
|
|
|
|
|
|
|