Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Select Distinct - Multiple Columns Expand / Collapse
Author
Message
Posted Wednesday, September 22, 2010 1:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 1:01 PM
Points: 75, Visits: 209
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.
Post #991548
Posted Thursday, September 23, 2010 8:09 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 10:05 AM
Points: 2,897, Visits: 5,981
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
Post #992050
Posted Wednesday, February 9, 2011 1:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, January 29, 2014 4:01 AM
Points: 17, Visits: 50
a
Post #1060887
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse