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 Tuesday, September 21, 2010 11:00 AM
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
I want to select ColumnA, ColumnB, ColumnC
but, I want to check for Distinct only on ColumnA.
I do need the other two columns because they are used in Calculated fields.

Thank you.
Post #990562
Posted Tuesday, September 21, 2010 11:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 20, 2014 5:34 AM
Points: 2,651, Visits: 5,990
Some sample data and required output would be very valuable here. See the first link in my signature for more info.

You need to figure out if you'll do the group in the SQL query or at report rendering. Also, if you're doing aggregations on the data and you want to do it in the dataset not at rendering you'll need to use a GROUP BY Clause not the DISTINCT Keyword.

-Luke.


To help us help you read this

For better help with performance problems please read this
Post #990606
Posted Tuesday, September 21, 2010 11:48 AM
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
Luke L (9/21/2010)
Some sample data and required output would be very valuable here. See the first link in my signature for more info.

You need to figure out if you'll do the group in the SQL query or at report rendering. Also, if you're doing aggregations on the data and you want to do it in the dataset not at rendering you'll need to use a GROUP BY Clause not the DISTINCT Keyword.

-Luke.


If possible, I want to do everything at rendering. I just want the records that I need returned in the dataset. Distinct only on columnA.

PS. I was wondering when you'd get to my post. I saw your sig moving up the list of posts.
Post #990614
Posted Tuesday, September 21, 2010 11:56 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 20, 2014 5:34 AM
Points: 2,651, Visits: 5,990
so the sample data and expected output?

If you're doing everything at rendering just pull everything, then group by columnA, add your calculations on columns b,c or whatnot. It's about the best I can do without something more concrete from you.

I'm not sure at rendering is the best place to do all of your aggregation but YMMV depending on how much data you're talking about. No need to pull 1000 rows across the network just to aggregate them down to 5 when you can do that at the database and pull your 5 records. While SSRS does a pretty good job of this, the larger the dataset, the longer it takes to get to the SSRS server across the network, and thus the longer the user waits on rendering.

-Luke.


To help us help you read this

For better help with performance problems please read this
Post #990623
Posted Tuesday, September 21, 2010 12:24 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
Luke L (9/21/2010)
so the sample data and expected output?

If you're doing everything at rendering just pull everything, then group by columnA, add your calculations on columns b,c or whatnot. It's about the best I can do without something more concrete from you.

I'm not sure at rendering is the best place to do all of your aggregation but YMMV depending on how much data you're talking about. No need to pull 1000 rows across the network just to aggregate them down to 5 when you can do that at the database and pull your 5 records. While SSRS does a pretty good job of this, the larger the dataset, the longer it takes to get to the SSRS server across the network, and thus the longer the user waits on rendering.

-Luke.


In fact, by selecting only those records which have a unique column A, I'm there, and at rendering I only do some calcs.

Record ColumnA ColumnB ColumnC
1 ABC1234 09/01/10 09/02/10
2 ABC1234 09/01/10 09/03/10
3 DEF1234 09/02/10 09/04/10
4 GHI1234 09/05/10 09/05/10
5 GHI1234 09/05/10 09/06/10

What I want is to return Record 1 or 2 (it doesn't matter which, I assume whichever is first in the database), Record 3, and Record 4 or 5.

The normal distinct will return all of these.

Thank you.
Post #990653
Posted Tuesday, September 21, 2010 12:28 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 20, 2014 5:34 AM
Points: 2,651, Visits: 5,990
Why doesn't it matter which in records 1-2? If it doesn't matter why return that column at all?

-Luke.


To help us help you read this

For better help with performance problems please read this
Post #990655
Posted Tuesday, September 21, 2010 12:37 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
Luke L (9/21/2010)
Why doesn't it matter which in records 1-2? If it doesn't matter why return that column at all?

-Luke.

Ok,
ColumnA is a shipping number.
Each shipping number may have several orders in it.
Therefore, it is repeated, once for every order. I did not include the order number column, since I don't need it. Each unique shipping number is a load.
I want to count how many loads there are, and for different delivery date time periods, hence columnB and columnC.
Post #990661
Posted Tuesday, September 21, 2010 12:43 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 20, 2014 5:34 AM
Points: 2,651, Visits: 5,990
tsmith-960032 (9/21/2010)
Luke L (9/21/2010)
Why doesn't it matter which in records 1-2? If it doesn't matter why return that column at all?

-Luke.

Ok,
ColumnA is a shipping number.
Each shipping number may have several orders in it.
Therefore, it is repeated, once for every order. I did not include the order number column, since I don't need it. Each unique shipping number is a load.
I want to count how many loads there are, and for different delivery date time periods, hence columnB and columnC.


Just to play a bit of devil's advocate here. What if instead of
1 ABC1234 09/01/10 09/02/10
2 ABC1234 09/01/10 09/03/10
rows 1 and 2 looked like this?

1 ABC1234 09/01/10 09/02/10
2 ABC1234 10/01/10 10/03/10

Depending on how you are calculating "different delivery date time periods" How do you know which date period to put shipment abc1234 into or do you want it to show up in both?


To help us help you read this

For better help with performance problems please read this
Post #990667
Posted Wednesday, September 22, 2010 8:25 AM
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
Luke L (9/21/2010)
tsmith-960032 (9/21/2010)
Luke L (9/21/2010)
Why doesn't it matter which in records 1-2? If it doesn't matter why return that column at all?

-Luke.

Ok,
ColumnA is a shipping number.
Each shipping number may have several orders in it.
Therefore, it is repeated, once for every order. I did not include the order number column, since I don't need it. Each unique shipping number is a load.
I want to count how many loads there are, and for different delivery date time periods, hence columnB and columnC.


Just to play a bit of devil's advocate here. What if instead of
1 ABC1234 09/01/10 09/02/10
2 ABC1234 09/01/10 09/03/10
rows 1 and 2 looked like this?

1 ABC1234 09/01/10 09/02/10
2 ABC1234 10/01/10 10/03/10

Depending on how you are calculating "different delivery date time periods" How do you know which date period to put shipment abc1234 into or do you want it to show up in both?


Luke, the returned data is in the past, not future. So, in order for example to render load statistics,
I would need to return DISTINCT shipping numbers to get total loads, and also use Column C for example, to check for time period delivered, for example previous year, current year, last month, etc.

Yes, I could say
Select Distinct ColumnA, Count(ColumnB) As CB, Count(ColumnC) As CC
From Table SoAndSo
Group By ColumnA

But then I would lose the use of Columns B & C
Post #991215
Posted Wednesday, September 22, 2010 10:46 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:36 PM
Points: 2,818, Visits: 2,565
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
Post #991369
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse