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 |
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-03-19 : 11:37:23
|
I have a child table cqt in which i wanted to get top 1 record's cqtDestinationEntry in result setI wanted to implemetn either by CTE or any thingMy query is SELECT csm.fkSegmentID, csm.csmContainerID, csmContainerGrossWeight, csmNumberofCopies, cqt.cqtDestinationEntry, row_number() over (partition by csm.fkSegmentID, csm.csmContainerID ORDER BY cqt.cqtDestinationEntry DESC) as Rn FROM SegmentRecord seg INNER JOIN ContainerSummaryRecord csm ON seg.fkJobID = csm.fkJobID AND seg.segSegmentID = csm.fkSegmentID inner Join ContainerQuantityRecord cqt on cqt.fkJobID = csm.fkJobID and cqt.fkContainerID = csm.csmContainerID where seg.fkJobID = 'omc00070' and csmContainerType = 'P' Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
X002548
Not Just a Number
15586 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-19 : 14:18:39
|
based on this...row_number() over (partition by csm.fkSegmentID, csm.csmContainerID ORDER BY cqt.cqtDestinationEntry DESC) as Rn... if you put a where condition Rn=1 you will get record with max cqtDestinationEntry value within each csm.fkSegmentID, csm.csmContainerID group------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|