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 |
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2013-11-01 : 10:29:46
|
I have this but how can I get all the columns of the row? I only want them distinct per these 2 cols.SELECT DISTINCT OrdHst.ordernbr, OrdNbr.trans FROM OrdHst JOIN OrdNbr ON OrdHst.ordernbr = OrdNbr.ordernbrORDER BY 1,2Could return:12345 00112345 AAA12345 BBB12345 CCC12345 PWB |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-11-01 : 11:13:15
|
Which of the possible multiple values from the other columns do you want to get? One way would be as shown below. Change the ORDER BY clause in the row_number function to control which values for the other columns get picked up:SELECT *FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY OrdHst.ordernbr, OrdNbr.trans ORDER BY OrdHst.ordernbr) AS RNFROM OrdHst JOIN OrdNbr ON OrdHst.ordernbr = OrdNbr.ordernbr) s WHERE RN = 1 |
 |
|
AdamWest
Constraint Violating Yak Guru
360 Posts |
Posted - 2013-11-01 : 11:30:52
|
Would want all the columns just distincting on the order# and trncdewhen trncde is one of these 3 (PDB, PWB, PBB).quote: Originally posted by James K Which of the possible multiple values from the other columns do you want to get? One way would be as shown below. Change the ORDER BY clause in the row_number function to control which values for the other columns get picked up:SELECT *FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY OrdHst.ordernbr, OrdNbr.trans ORDER BY OrdHst.ordernbr) AS RNFROM OrdHst JOIN OrdNbr ON OrdHst.ordernbr = OrdNbr.ordernbr) s WHERE RN = 1
|
 |
|
|
|
|