|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:54 AM
Points: 7,086,
Visits: 7,138
|
|
Luke L (7/27/2010) 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.
Just me being stupid. I somehow read it an a totally insane way.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 5,235,
Visits: 7,036
|
|
Carlo Romagnano (7/27/2010)
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 of columns.  Yes, of course - I typed columns, but intended to write rows. Thanks for catching my mistake.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, November 19, 2012 11:30 PM
Points: 290,
Visits: 713
|
|
Explanation: The distinct clause guarantees all output records will be unique, irrespective of how you write the select statement.
CREATE TABLE TABLE2( Col1 int ,Col2 int)
INSERT INTO table2 VALUES(1,3) INSERT INTO table2 VALUES(1,4)
SELECT DISTINCT Col1,Col2 FROM table2
I agree the use of parenthesis wont affect the output. But while using 2 columns with DISTINCT can produce non unique rows (ateleast in one columm... )
John
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 5,235,
Visits: 7,036
|
|
$partacu$ (8/2/2010)
But while using 2 columns with DISTINCT can produce non unique rows (ateleast in one columm...  ) As long as there is a difference in at least one column, the rows are not identical.
(Or would you describe a blond-haired, blue-eyed guy, aged 34, weighing 95 kg, and measuring 1.95 m as "identical to" a blond-haired, brown-eyed girl, aged 7, weighing 23 kg and measuring 1.23 m, just because they both have blond hair?)
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, July 25, 2012 9:04 PM
Points: 542,
Visits: 187
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 9:43 AM
Points: 469,
Visits: 193
|
|
| Nice and easy question that should result to same answer regardless due to the use of the SELECT DISTINCT. Thanks.
|
|
|
|