SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 distinct but include all the columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AdamWest
Constraint Violating Yak Guru

USA
351 Posts

Posted - 11/01/2013 :  10:29:46  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3559 Posts

Posted - 11/01/2013 :  11:13:15  Show Profile  Reply with Quote
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

USA
351 Posts

Posted - 11/01/2013 :  11:30:52  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000