Solved

SQL Join - Select DISTINCT rows headache

Posted on 2009-05-14
8
319 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

730 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