Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select Distinct - Multiple Columns


Select Distinct - Multiple Columns

Author
Message
tsmith-960032
tsmith-960032
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 217
Daniel Bowlin (9/22/2010)
Try a CTE or temp table to get your DISTINCT then join that back to a SELECT where you query the data and constrain on the MIN or MAX of Column C


Thank you for your reply.
I solved my issue as follows :

Select Distinct ColumnA, Max(ColumnB) As ColumnB, Max(ColumnC) As ColumnC
From SoAndSoTable
Group By ColumnA

Thanks again Daniel, and of course Thank You Luke.
Luke L
Luke L
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2686 Visits: 6103
tsmith-960032 (9/22/2010)
Daniel Bowlin (9/22/2010)
Try a CTE or temp table to get your DISTINCT then join that back to a SELECT where you query the data and constrain on the MIN or MAX of Column C


Thank you for your reply.
I solved my issue as follows :

Select Distinct ColumnA, Max(ColumnB) As ColumnB, Max(ColumnC) As ColumnC
From SoAndSoTable
Group By ColumnA

Thanks again Daniel, and of course Thank You Luke.


Sorry, for the late reply, but yes that's why I was trying to get at. When you do your grouping you need to decide which results you want from Columns b and c. The MAX() function will get you there.

Glad it worked out for you, however using the DISTINCT Keyword and a GROUP BY clause is redundant and I'm somewhat amazed that SQL doesn't throw an error here. I understand that it doesn't 'cause I just tested it, but it still seems like something strange and perhaps may cause you issues down the road. I'd remove the DISTINCT Keyword and just stick with the GROUP BY clause for clarity.

-Luke.

To help us help you read this

For better help with performance problems please read this
guru.patil
guru.patil
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 69
a
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search