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 |
shailesh18
Starting Member
8 Posts |
Posted - 2011-10-03 : 04:20:32
|
Dear friendsKindly help me with this queryI have 2 table P_S_T and AND P_S_CertificationStatus_TP_S_T---------------PID SID ---------------1 1 1 2 1 3 2 1 2 5 --------------- P_S_CertificationStatus_T------------------------------------PID SID CID Cdt_DT (dd-MM-yy)-------------------------------------1 1 2 01-08-111 1 1 01-01-111 2 2 05-03-112 5 5 18-06-112 5 5 18-03-11-------------------------------------- The Output, i want is ------------------------------------PID SID CID Cdt_DT (dd-MM-yy)-------------------------------------1 1 2 01-08-111 2 2 05-03-111 3 NULL NULL2 1 NULL NULL2 5 5 18-06-11-------------------------------------- Red color indicates row is not in table P_S_CertificationStatus_TAgain, from table 2 i.e P_S_CertificationStatus_T, its only printing CID and Cdt_DT which is latest sorted date wise descending.Kindly let me know, how to achieve this.With reference to the above cited issue, i know, how to sort the records and take the latest from P_S_CertificationStatus_T, i am somehow failing in JOINS.http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=166162 Thanks in advance. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-03 : 04:47:24
|
[code]SELECT pst.PID,pst.SID,psc.CID,psc.Cdt_DTFROM P_S_T pstLEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY PID,SID ORDER BY Cdt_DT DESC) AS Rn,*FROM P_S_CertificationStatus_T)pscON psc.SID= pst.SIDAND psc.PID = pst.PIDAND psc.Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
shailesh18
Starting Member
8 Posts |
Posted - 2011-10-03 : 05:36:02
|
Dear VisakhGenius.Would you please explain me, how it works.Thanks for wonderful solution.Thank you very much.  quote: Originally posted by visakh16
SELECT pst.PID,pst.SID,psc.CID,psc.Cdt_DTFROM P_S_T pstLEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY PID,SID ORDER BY Cdt_DT DESC) AS Rn,*FROM P_S_CertificationStatus_T)pscON psc.SID= pst.SIDAND psc.PID = pst.PIDAND psc.Rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-03 : 06:07:39
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|