简体   繁体   中英

SQL Distinct and * combination under

I know what select count(distinct country) from customer does.

But I can't understand what this

SELECT *, COUNT(DISTINCT Country) 
FROM Customers;

does.

Can anyone explain?

(use this link to enter the codes which I am asking about) https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct2

It does nothing:

create table #test (Country varchar(10), Otherthings varchar(20))
insert into #test(Country,Otherthings) values ('UK','Something'),('USA','Something else')

SELECT *, COUNT(DISTINCT Country) 
FROM #test

Produces:

> Column '#test.Country' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
SELECT *, COUNT(DISTINCT Country) FROM Customers;

Will provide you a row with a column, count(number of unique country's name). And I don't think it will create an error, like someone said. I tried it and it's working.

you get the count of countries in the customer Table.. here DISTINCT keyword is used for avoiding duplication count. example: country list is INDIA,INDIA,SPAIN,GERMANY,SPAIN then the count is 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM