如何使用SQL Server计算几个类别(不计算行)

时间:2019-04-29 03:06:20

标签: sql sql-server

假设我有一个这样的表:

 country   province   city   people
 __________________________________
 country1  province1  city1   200 
 country1  province1  city2   150 
 country1  province2  city3   350
 country2  province3  city4   75

我需要显示我有多少国家,多少省,多少城市和多少人。然后,我需要显示每个城市,每个省和每个国家有多少人。

我已经尝试过Count(Distinct),但是它不能与分区一起使用。

5 个答案:

答案 0 :(得分:0)

也许GROUP BYGROUPING SETS可以为您提供帮助:

SELECT
  --GROUPING_ID(country,province,city),
  CASE GROUPING_ID(country,province,city)
    WHEN 7 THEN 'total'
    WHEN 3 THEN 'country total - '+country
    WHEN 1 THEN 'province total - '+province
    ELSE 'city - '+city
  END row_title,
  country,
  province,
  city,
  COUNT(DISTINCT province) province_count,
  COUNT(DISTINCT city) city_count,
  SUM(people) people_count
FROM YourTable
GROUP BY GROUPING SETS(
      (country,province,city),
      (country,province),
      (country),
      ()
    )
ORDER BY country,province,city

测试数据:

CREATE TABLE YourTable(country varchar(20),province varchar(20),city varchar(20),people int)

INSERT YourTable(country,province,city,people)VALUES
('country1','province1','city1',200),
('country1','province1','city2',150),
('country1','province2','city3',350),
('country2','province3','city4',75)

也尝试使用以下内容:

SELECT column_name,value
FROM
  (
    SELECT
      COUNT(DISTINCT province) [province count],
      COUNT(DISTINCT city) [city count],
      SUM(people) [people count]
    FROM YourTable
  ) q UNPIVOT(value FOR column_name IN([province count],[city count],[people count])) u

答案 1 :(得分:0)

你很亲密,安迪。 Count(Distinct ...)是正确的方法。

如果您想知道有多少个国家,请使用:

select count(distinct(country)) from yourtable;

有多少省?城市计数与省相似。

select count(distinct(province)) from yourtable;

有多少人?

select sum(people) from yourtable;

对于人来说,您不能只算一算。您将必须对所有数字求和。

答案 2 :(得分:0)

您可以尝试以下方法根据需要获取计数:

CREATE TABLE #YourTable(country varchar(20),province varchar(20),city varchar(20),people int)

INSERT #YourTable(country,province,city,people)VALUES
('country1','province1','city1',200),
('country1','province1','city2',150),
('country1','province2','city3',350),
('country2','province3','city4',75)

SELECT 'Distinct Countries' AS type, COUNT(DISTINCT country) from #YourTable
UNION ALL
SELECT 'Distinct province' AS type, COUNT(DISTINCT province) from #YourTable
UNION ALL
SELECT 'Distinct City' AS type, COUNT(DISTINCT city) from #YourTable
UNION ALL
SELECT CONCAT('Distinct ', Country,' ', province,' ', city), count(people)
FROM #YourTable
GROUP BY ROLLUP(country, province, city) 

答案 3 :(得分:0)

以下脚本将在一行中返回所有结果。但是,如果要将每个结果放在单独的行中,则可能需要使用UNION来获得预期的结果集。

SELECT COUNT(DISTINCT Country) [Number of Country],
COUNT(DISTINCT Province) [Number of Province],
COUNT(DISTINCT City) [Number of City],
SUM(People) [Total People]
FROM (
    VALUES
            ('country1','province1','city1',200),
            ('country1','province1','city2',150 ),
            ('country1','province2','city3',350),
            ('country2','province3','city4',75)
    ) 
    V([Country],[Province],City,People);

答案 4 :(得分:0)

我不明白为什么您需要count(distinct)作为人口。怎么样?

select country, province, city, sum(people) 
from t
group by grouping sets ( (country, province, city), (country, province), (country) );

我在您的样本数据中看不到有人被重复计算的证据。

要计算每个组的数量,需要执行以下操作:

select count(distinct country) as num_countries,
       count(distinct country + ':' + province) as num_provinces,
       count(distinct country + ':' + province + ':' + city) as num_cities

SQL Server不允许count(distinct)使用多个参数。