Solved

SQL Join - Select DISTINCT rows headache

Posted on 2009-05-14
8
316 Views
Last Modified: 2012-05-07
I've got a table as follows with Id's and Colours.

Id Colours
0 Red
1 Blue
2 Red
3 Green
4 Red
5 Red
6 Green
7 Blue
8 Green

In the table there are multiple Colours with different Ids.

I want a query to produce just distinct colours, with any Id that is associated with it.

e.g.
Id Colour
0 Red
7 Blue
3 Green

I've tried DISTINCT, but because the IDs are always different that doesn't work.

I have a feeling I need to JOIN it on itself, somehow, with a GROUP BY statement on the colour.  But as soon as I add the GROUP BY, it needs the ID included, which destroys the GROUP BY.

Could anyone give me a point in the right direction?
0
Comment
Question by:TNGIT
  • 4
  • 2
  • 2
8 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24385350
Hope this helps

SELECT min(Id) AS Id, Colours
FROM urtable
GROUP BY Colours
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24385362
SELECT MAX(ID) AS ID, Colour
FROM Your_Table_Name
GROUP BY Colour
0
 

Author Comment

by:TNGIT
ID: 24385372
Many thanks, and apologies - the Id field is in fact a uniqueidentifier field.

MAX and MIN don't want to work with it.  Is there a similar function for uniqueidentifier fields?
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 250 total points
ID: 24385408
Hope this helps:

SELECT Id, Colours
FROM (
SELECT Id, Colours, row_number() OVER ( partition BY Colours ORDER BY Id) rnum
FROM urtable)
WHERE rnum = 1
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24385429
You will have to have an alias on the derived table, but rrjegan17's suggestion should work!
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 24385453

SELECT Id, Colours

FROM (

    SELECT Id, Colours

    , row_number() OVER ( partition BY Colours ORDER BY Id) rnum

    FROM urtable

) AS t

WHERE rnum = 1

Open in new window

0
 

Author Closing Comment

by:TNGIT
ID: 31581470
Fantastic!
Many thanks, I was beginning to go slightly mad trying different things.  I've never heard or used the OVER or PARTITION words.
I'll read into them now.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24385525
TNGIT,

The OVER analytical clause has been very useful to me and definitely worth looking into IMHO:
http://msdn.microsoft.com/en-us/library/ms189461.aspx

Glad we could help.

Happy coding!

Best regards,
Kevin
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now