Solved

SQL Join - Select DISTINCT rows headache

Posted on 2009-05-14
8
315 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now