﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Reporting Services / Reporting Services  / Select Distinct - Multiple Columns / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 06:35:09 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>a</description><pubDate>Wed, 09 Feb 2011 01:13:09 GMT</pubDate><dc:creator>guru.patil</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]tsmith-960032 (9/22/2010)[/b][hr][quote][b]Daniel Bowlin (9/22/2010)[/b][hr]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[/quote]Thank you for your reply.I solved my issue as follows :Select Distinct ColumnA, Max(ColumnB) As ColumnB, Max(ColumnC) As ColumnCFrom SoAndSoTableGroup By ColumnAThanks again Daniel, and of course Thank You Luke.[/quote]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.</description><pubDate>Thu, 23 Sep 2010 08:09:31 GMT</pubDate><dc:creator>Luke L</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]Daniel Bowlin (9/22/2010)[/b][hr]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[/quote]Thank you for your reply.I solved my issue as follows :Select Distinct ColumnA, Max(ColumnB) As ColumnB, Max(ColumnC) As ColumnCFrom SoAndSoTableGroup By ColumnAThanks again Daniel, and of course Thank You Luke.</description><pubDate>Wed, 22 Sep 2010 13:55:01 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>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</description><pubDate>Wed, 22 Sep 2010 10:46:47 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]Luke L (9/21/2010)[/b][hr][quote][b]tsmith-960032 (9/21/2010)[/b][hr][quote][b]Luke L (9/21/2010)[/b][hr]Why doesn't it matter which in records 1-2?  If it doesn't matter why return that column at all?  -Luke.[/quote]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.[/quote]Just to play a bit of devil's advocate here.  What if instead of 1 ABC1234 09/01/10 09/02/102 ABC1234 09/01/10 09/03/10rows 1 and 2 looked like this?1 ABC1234 09/01/10 09/02/102 ABC1234 10/01/10 10/03/10Depending 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?[/quote]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 saySelect Distinct ColumnA, Count(ColumnB) As CB, Count(ColumnC) As CCFrom Table SoAndSoGroup By ColumnABut then I would lose the use of Columns B &amp; C</description><pubDate>Wed, 22 Sep 2010 08:25:03 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]tsmith-960032 (9/21/2010)[/b][hr][quote][b]Luke L (9/21/2010)[/b][hr]Why doesn't it matter which in records 1-2?  If it doesn't matter why return that column at all?  -Luke.[/quote]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.[/quote]Just to play a bit of devil's advocate here.  What if instead of 1 ABC1234 09/01/10 09/02/102 ABC1234 09/01/10 09/03/10rows 1 and 2 looked like this?1 ABC1234 09/01/10 09/02/102 ABC1234 10/01/10 10/03/10Depending 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?</description><pubDate>Tue, 21 Sep 2010 12:43:32 GMT</pubDate><dc:creator>Luke L</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]Luke L (9/21/2010)[/b][hr]Why doesn't it matter which in records 1-2?  If it doesn't matter why return that column at all?  -Luke.[/quote]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.</description><pubDate>Tue, 21 Sep 2010 12:37:40 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>Why doesn't it matter which in records 1-2?  If it doesn't matter why return that column at all?  -Luke.</description><pubDate>Tue, 21 Sep 2010 12:28:13 GMT</pubDate><dc:creator>Luke L</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]Luke L (9/21/2010)[/b][hr]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.[/quote]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/10What 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.</description><pubDate>Tue, 21 Sep 2010 12:24:12 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>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.</description><pubDate>Tue, 21 Sep 2010 11:56:03 GMT</pubDate><dc:creator>Luke L</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>[quote][b]Luke L (9/21/2010)[/b][hr]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.[/quote]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.:-P</description><pubDate>Tue, 21 Sep 2010 11:48:53 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item><item><title>RE: Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>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.</description><pubDate>Tue, 21 Sep 2010 11:41:58 GMT</pubDate><dc:creator>Luke L</dc:creator></item><item><title>Select Distinct - Multiple Columns</title><link>http://www.sqlservercentral.com/Forums/Topic990562-150-1.aspx</link><description>I want to select ColumnA, ColumnB, ColumnCbut, 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.</description><pubDate>Tue, 21 Sep 2010 11:00:46 GMT</pubDate><dc:creator>tsmith-960032</dc:creator></item></channel></rss>