| Author |
Topic |
|
hari4u6
Starting Member
12 Posts |
Posted - 2009-12-21 : 01:02:20
|
| Hello all, i want to get the latest value for a given id, coz i want to use tat value to insert in a new record. Hari. |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-21 : 01:28:11
|
| Post your table and expected outputSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
hari4u6
Starting Member
12 Posts |
Posted - 2009-12-21 : 02:34:42
|
| ForumReplyId ForumThreadId Subject Description------------------------------------------------1 1 FAQ dasdadad2 1 Re:FAQ adsadasad Expected output for threadId '1' subject Re:FAQForumReplyId is Autogenerated.Hari. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-21 : 03:29:50
|
| Try this..select * from table_name where ForumReplyId =(select max(ForumReplyId) from table_name where ForumThreadId =1)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-21 : 03:46:00
|
HiSELECT FORUMREPLYID,FORUMTHREADID,SUBJECT,DESCRIPTION FROM(SELECT * , ROW_NUMBER() OVER(PARTITION BY FORUMTHREADID ORDER BY FORUMREPLYID DESC) AS SEQFROM #TEMP) TWHERE T.SEQ = 1 -------------------------R... |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-21 : 03:56:45
|
quote: Originally posted by senthil_nagore Try this..select * from table_name where ForumReplyId =(select max(ForumReplyId) from table_name where ForumThreadId =1)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
can you test your query with below data set..Create table #temp (ForumReplyId INT,ForumThreadId INT, Subject varchar(50),Description varchar(50))Insert into #tempselect 1 ,1 ,'FAQ' ,'first' union allselect 2 ,1 ,'Re:FAQ' ,'firstmaximum' union allselect 1 ,2 ,'FAQ' ,'second' union allselect 2 ,2 ,'Re:FAQ' ,'secondmaximum' -------------------------R... |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-21 : 04:03:39
|
quote: Originally posted by rajdaksha
quote: Originally posted by senthil_nagore Try this..select * from table_name where ForumReplyId =(select max(ForumReplyId) from table_name where ForumThreadId =1)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
can you test your query with below data set..Create table #temp (ForumReplyId INT,ForumThreadId INT, Subject varchar(50),Description varchar(50))Insert into #tempselect 1 ,1 ,'FAQ' ,'first' union allselect 2 ,1 ,'Re:FAQ' ,'firstmaximum' union allselect 1 ,2 ,'FAQ' ,'second' union allselect 2 ,2 ,'Re:FAQ' ,'secondmaximum' -------------------------R...
I miss the condition, thanks to point outselect * from table_name where ForumReplyId =(select max(ForumReplyId) from table_name where ForumThreadId =1)and ForumThreadId =1Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-21 : 04:31:37
|
HiTry this for all data set'sSELECT FORUMREPLYID,FORUMTHREADID,SUBJECT,DESCRIPTION FROM(SELECT * , ROW_NUMBER() OVER(PARTITION BY FORUMTHREADID ORDER BY FORUMREPLYID DESC) AS SEQFROM #TEMP) TWHERE T.SEQ = 1 -------------------------R... |
 |
|
|
|