|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 10:31 AM
Points: 18,857,
Visits: 12,442
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 12:24 PM
Points: 2,892,
Visits: 5,871
|
|
webrunner (7/26/2010) Good question, interesting to learn that the parentheses make no difference for SELECT DISTINCT.
I have one question, though. I've seen people show me queries where they write SELECT COUNT(DISTINCT userid) or something like that. Is that also the same as SELECT DISTINCT COUNT(userid), or is there a difference?
And I am also curious about how (1) SELECT COUNT(DISTINCT userid) would differ from (2) SELECT COUNT(userid) - I assume if there are multiple rows with the same userid in the table being queried, the second query would return more rows than the first? **
** Edited again - sorry, I guess these queries would return the same number of rows (e.g. one summary row), but maybe different COUNT values. Is that correct?
Thanks, webrunner
AS per The Count() BOL Entry The Distinct Keyword is a argument of the Count() Function and is not the same as when used like SELECT DISTINCT col1, col2 from mytable; For that use reference the SELECT Clause BOL Entry that states: DISTINCT
Specifies that only unique rows can appear in the result set. Null values are considered equal for the purposes of the DISTINCT keyword. -Luke.
Edited to fix quote.
To help us help you read this
For better help with performance problems please read this
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 2:08 PM
Points: 2,121,
Visits: 2,226
|
|
bitbucket-25253 (7/26/2010)
webrunner Easy enough to find out ... CREATE TABLE #T(userid INT) INSERT INTO #T SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
SELECT COUNT(DISTINCT userid) AS '(1)' FROM #T SELECT DISTINCT COUNT(userid)AS '(2)' FROM #T Results: (1) (2) 7 8
Thanks - that's a nice illustration of the difference.
- webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 2:08 PM
Points: 2,121,
Visits: 2,226
|
|
Luke L (7/26/2010)
webrunner (7/26/2010) Good question, interesting to learn that the parentheses make no difference for SELECT DISTINCT.
I have one question, though. I've seen people show me queries where they write SELECT COUNT(DISTINCT userid) or something like that. Is that also the same as SELECT DISTINCT COUNT(userid), or is there a difference?
And I am also curious about how (1) SELECT COUNT(DISTINCT userid) would differ from (2) SELECT COUNT(userid) - I assume if there are multiple rows with the same userid in the table being queried, the second query would return more rows than the first? **
** Edited again - sorry, I guess these queries would return the same number of rows (e.g. one summary row), but maybe different COUNT values. Is that correct?
Thanks, webrunnerAS per The Count() BOL EntryThe Distinct Keyword is a argument of the Count() Function and is not the same as when used like SELECT DISTINCT col1, col2 from mytable; For that use reference the SELECT Clause BOL Entry that states: DISTINCT
-Luke. Specifies that only unique rows can appear in the result set. Null values are considered equal for the purposes of the DISTINCT keyword.
Thanks, Luke. I also learned two more things from the COUNT BOL entry: (1) "COUNT(*) returns the number of rows in a specified table without getting rid of duplicates" and (2) "For return values greater than 2^31-1, COUNT produces an error. Use COUNT_BIG instead." I hadn't even known about COUNT_BIG before.
This is all fundamental knowledge that I need to master. It's amazing how many things commonly seen as "basic" or "SQL 101" require careful study to avoid getting tripped up on them.
Thanks again, webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 2,163,
Visits: 2,151
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 10:45 AM
Points: 5,296,
Visits: 7,238
|
|
A good question, highlighting a very common misconception. I've seen so many people think that there is a way to limit DISTINCT to only some of the SELECTed columns - and yet, when I ask them what SQL Server should return for the other columns, they are never able to answer.
webrunner (7/26/2010) I have one question, though. I've seen people show me queries where they write SELECT COUNT(DISTINCT userid) or something like that. Is that also the same as SELECT DISTINCT COUNT(userid), or is there a difference? COUNT(*) returns the number of columns (regardless of values); COUNT(userid) counts the number of non-NULL vallues in the userid column; and COUNT(DISTINCT userid) counts the number of distinct non-NULL values in the userid column. This DISTINCT keyword is different from the one at the start of the SELECT list.
SELECT DISTINCT .... means that at the end of the query evaluation, rows that are complete duplicates of another row (in all columns) are removed.
So SELECT DISTINCT COUNT(userid) FROM SomeTable will first count the number of rows where userid is not NULL, yielding a single row as result (with that number as the value in its only column). The DISTINCT will then remove duplicates - which don't exist as the COUNT without GROUP BY guarantees a single row result set.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 7,185,
Visits: 7,285
|
|
bitbucket-25253 (7/26/2010)
webrunner Easy enough to find out ... CREATE TABLE #T(userid INT) INSERT INTO #T SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
SELECT COUNT(DISTINCT userid) AS '(1)' FROM #T SELECT DISTINCT COUNT(userid)AS '(2)' FROM #T Results: (1) (2) 7 8
Looks wrong to me. Surely that 8 should be a 1, or the DISTINCT keyword ahould not be present in the second select?
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 10:27 AM
Points: 1,384,
Visits: 4,881
|
|
A bit of a strange question. I'd never come across the user of parantheses with DISTINCT so assumed it must be some sort of extended syntax I didn't know about. So guessed wrongly that it must have different behaviour. But it turned out that the reason I'd never come across it was that there is no alternative syntax at all! So an interesting problem to think about, but I can't say I learned anything from it (other than that there are apparently people out there who unnecessarily put brackets with a distinct!)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 12:24 PM
Points: 2,892,
Visits: 5,871
|
|
Tom.Thomson (7/26/2010)
bitbucket-25253 (7/26/2010)
webrunner Easy enough to find out ... CREATE TABLE #T(userid INT) INSERT INTO #T SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
SELECT COUNT(DISTINCT userid) AS '(1)' FROM #T SELECT DISTINCT COUNT(userid)AS '(2)' FROM #T Results: (1) (2) 7 8 Looks wrong to me. Surely that 8 should be a 1, or the DISTINCT keyword ahould not be present in the second select?
Nope, run the code you get 7 and 8 respectively. The Distinct in the second query is applied after the rows are created. So since there is no group by clause it counts all of the userid's and returns 1 row. The DISTINCT in that instance makes sure there are distinct rows, which there are because there is only one. So Yes the DISTINCT keyword doesn't really do anything in the second select, but that was more or less the point of the example that bitbucket gave, to illustrate the difference between Count(DISTINCT expression ) and SELECT DISTINCT count(expression).
-Luke.
To help us help you read this
For better help with performance problems please read this
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 1,996,
Visits: 1,864
|
|
Hugo Kornelis (7/26/2010)COUNT(*) returns the number of columns (regardless of values); That's false: COUNT(*) returns the number of ROWS regardless of values of columns.
|
|
|
|