Solved

SQL Join - Select DISTINCT rows headache

Posted on 2009-05-14
8
318 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
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.

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

825 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