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 |
|
messedup
Starting Member
2 Posts |
Posted - 2010-04-27 : 00:54:38
|
| So far I have returned a list that looks like this:cola colb colcx hair Nox finger Nox hair Yesusing distinct and group by and order by I only want the first result of cola and colb.desired result is:cola colb colcx hair Nox finger No I cant figure out how to use top 1, or something similiar to get my desired resultsThank you for taking time to look at my issue.The actual full query as i have it is below:select distinct JTrequestServiceTaskLink.RequestServiceTaskLinkID, JTItemType.ItemType,(Select YesNo=Case when isnull(NoResult,'') <> 'Negative' then'No'else 'Yes' End from JTResultAlcAcetone A where A.ResultID= JTREsultAlcAcetone.ResultID) as YesNofrom JTREsultAlcAcetone join JTResult on JTREsultAlcAcetone.resultid =JTResult.ResultIDjoin JTRequestServiceTaskItemLink on JTRequestServiceTaskItemLink.requestservicetaskitemlinkid=JTresult.requestservicetaskitemlinkidjoin JTrequestServiceTaskLink on JTrequestServiceTaskLink.requestservicetasklinkid =JTRequestServiceTaskItemLink.requestservicetasklinkidjoin JTItem on JTItem.ItemID=JTRequestServiceTaskItemLink.ItemIDjoin JTItemTYpeon JTItemType.ItemTYpeID=JTItem.ItemTYpeIDgroup by JTREsultAlcAcetone.NoResult, JTItemType.ItemTypeID,JTREsultAlcAcetone.ResultID,ItemTYpe,JTrequestServiceTaskLink.RequestServiceTaskLinkId, JTItem.ItemID,JTRequestServiceTaskItemLink.ItemIDorder by YesNo Asc |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 01:03:50
|
| what according to you represents the order? how do you determine that x hair with No is first record? there's no concept of first and last in sql table unless you spcify order in terms of some other field (by means of order by). do you have any such column?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
messedup
Starting Member
2 Posts |
Posted - 2010-04-27 : 01:08:37
|
| I am ordering by YesNo (colc in the example)So all the No's should be listed before the Yes' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 01:38:07
|
ok then use this:-SELECT cola,colb,colcFROM (SELECT ROW_NUMBER() OVER (PARTITION BY cola,colb ORDER BY colc) AS Seq,cola,colb,colc FROM Table)tWHERE Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|