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
 Help with Distinct Query

Author  Topic 

RichardSteele
Posting Yak Master

160 Posts

Posted - 2007-08-15 : 19:12:00
Here's my query:

SELECT NotYetPublished, Available, Catalogx, pubdate, pubdate_full, NYP, Fulfillment, Title from finaldata where available <> 'Out-of-Print' or (totalnew <>0 and totalused <>0 and totalcollectible<>0) order by NYP desc, pubdate_full desc, Fulfillment desc, Title

I only want to return records that do not have the same Catalogx. I can't use distinct since some of the other data is different.

How can I accomplish this? Thanks in advance.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-15 : 19:16:03
Are you using SQL Server 2000 or SQL Server 2005?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-15 : 19:26:54
If you are using SQL Server 2005, you can use the ROW_NUMBER() function.

SELECT NotYetPublished, Available, Catalogx, pubdate, pubdate_full, NYP, Fulfillment, Title
from (
SELECT NotYetPublished, Available, Catalogx, pubdate, pubdate_full, NYP, Fulfillment, Title, row_number() over (partition by catalogx) as recid from finaldata where available <> 'Out-of-Print' or (totalnew <>0 and totalused <>0 and totalcollectible<>0)
) as d
where recid = 1
order by NYP desc, pubdate_full desc, Fulfillment desc, Title


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

RichardSteele
Posting Yak Master

160 Posts

Posted - 2007-08-15 : 19:37:13
What would this look like for SQL 2000?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-16 : 01:47:19
Refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -