?
Solved

SQL Join - Select DISTINCT rows headache

Posted on 2009-05-14
8
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60

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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 1000 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
 
LVL 60

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 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 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 60

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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