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
 use DISTINCT but keep a corresponding value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jorondo
Starting Member

Canada
3 Posts

Posted - 07/13/2012 :  11:37:41  Show Profile  Reply with Quote
Hi,

I am quite new to SQL and have been searching for an answer to this for a while now. So here is my question:

I have variables X,Y,Z. X has many duplicates, with a numeric value on Y and Z. I want to SELECT DISTINCT X, max(Y), /*and here comes my question*/ is it possible to retrieve at the same time the Z value that corresponds to the max(Y) value?

I don't quite know how to formulate this well so I found nothing on google and I'd be glad if somebody could help me, or at least tell me if this is possible.

Thanks a lot!

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/13/2012 :  13:10:32  Show Profile  Reply with Quote
You can use the row_number function like this:
SELECT X,Y,Z FROM
(
	SELECT X,Y,Z,
		ROW_NUMBER() OVER (PARTITION BY X ORDER BY Y DESC,Z DESC) AS RN
	FROM 
		YourTable
)s
WHERE RN = 1;
Go to Top of Page

jorondo
Starting Member

Canada
3 Posts

Posted - 07/20/2012 :  10:51:38  Show Profile  Reply with Quote
Thanks, from the look of it it seems it will do just the right thing. However when I code it the only error that comes up is not recognizing the "over" statement. I should mention i am coding on SAS, maybe that changes something. Here is the error message,

Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=,
<>, =, >, >=, ?, AND, BETWEEN, CONTAINS, EQ, EQT, GE, GET, GT, GTT, LE, LET, LIKE,
LT, LTT, NE, NET, OR, ^=, |, ||, ~=.

Any idea?

quote:
Originally posted by sunitabeck

You can use the row_number function like this:
SELECT X,Y,Z FROM
(
	SELECT X,Y,Z,
		ROW_NUMBER() OVER (PARTITION BY X ORDER BY Y DESC,Z DESC) AS RN
	FROM 
		YourTable
)s
WHERE RN = 1;


Go to Top of Page

jorondo
Starting Member

Canada
3 Posts

Posted - 07/20/2012 :  10:57:04  Show Profile  Reply with Quote
here is my code, perhaps you'll see something I missed...

	create table thien_14_pat as
		select  b.enrolid, b.svcdate as first_disp, b.clopi, b.pras, b.tica from
		(select  b.enrolid, b.svcdate, b.clopi, b.pras, b.tica,
			row_number() over 
			(partition by b.enrolid order by b.svcdate, b.clopi desc, b.pras desc, b.tica desc) as RN
				from out.last_elig_18 as a inner join thien_14 as b
		)
					on a.enrolid=b.enrolid
						where (RN=1 and (a.indexdt<=b.svcdate<(a.index_dis+14)))
	group by enrolid;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 07/20/2012 :  14:11:16  Show Profile  Reply with Quote
SQL server doesnt have create table...as syntax. either it should be create view...as or you should use


select ....into table from....

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 07/20/2012 :  14:28:57  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by sunitabeck

You can use the row_number function like this:
SELECT X,Y,Z FROM
(
	SELECT X,Y,Z,
		ROW_NUMBER() OVER (PARTITION BY X ORDER BY Y DESC,Z DESC) AS RN
	FROM 
		YourTable
)s
WHERE RN = 1;



This was your post #3333


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/20/2012 :  19:23:49  Show Profile  Reply with Quote
quote:
This was your post #3333
Oops! I lost it before I could see it and capture it!! :) Now I am on 3335!!
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.05 seconds. Powered By: Snitz Forums 2000