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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 distinct but include all the columns

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.ordernbr
ORDER BY 1,2

Could return:

12345 001
12345 AAA
12345 BBB
12345 CCC
12345 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 RN
FROM OrdHst JOIN OrdNbr
ON OrdHst.ordernbr = OrdNbr.ordernbr
) s WHERE RN = 1
Go to Top of Page

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 trncde
when 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 RN
FROM OrdHst JOIN OrdNbr
ON OrdHst.ordernbr = OrdNbr.ordernbr
) s WHERE RN = 1


Go to Top of Page
   

- Advertisement -