简体   繁体   English

SQL 查询以获取同一表中列的不同值的计数

[英]SQL query to get count of distinct values of columns in a same table

I have table with columns like Gender, Status.我有包含性别、状态等列的表格。

The table value is something like this表值是这样的

ID ID Gender性别 Status地位
1 1个 Male男性 A01 A01
2 2个 Male男性
3 3个 Female女性 A02 A02
4 4个 Female女性
5 5个 Unknown未知
6 6个 Male男性
7 7 Female女性
8 8个 Unknown未知

I want to display我要展示

Gender性别 Status地位 Count数数
Male男性 A01 A01 1 1个
Female女性 A02 A02 1 1个
Unknown未知 0 0

I tried我试过了

SELECT 
    t3.Gender, t3.Status, COUNT(*) AS count 
FROM
    (SELECT DISTINCT
         t1.Gender, t1.Status 
     FROM 
         Consumer AS t1
     CROSS JOIN 
         Consumer AS t2 
     WHERE 
         t1.Status <> t2.Status 
         OR t1.Status <> t2.Status) AS t3 
GROUP BY
    t3.Gender, t3.Status

The final output something I want to display is我要显示的最终 output 是在此处输入图像描述 Please help on this.请帮忙解决这个问题。 Thanks谢谢

Since the desired results have now been updated, the solution is now much different.由于现在已经更新了所需的结果,因此解决方案现在有很大不同。

Try:尝试:

SELECT
  CASE WHEN GROUPING(Status) = 1 THEN 'Total' ELSE Status END AS Status,
  --ISNULL(Status, 'Total') AS Status, -- This would not work is actual Status might be null
  COUNT(CASE WHEN Gender = 'Female' THEN 1 END) AS Female,
  COUNT(CASE WHEN Gender = 'Male' THEN 1 END) AS Male,
  COUNT(CASE WHEN Gender = 'Unknown' THEN 1 END) AS Unknown,
  COUNT(*) AS Count
FROM @Consumer
WHERE Status > ''
GROUP BY Status WITH Rollup
ORDER BY GROUPING(Status), Status

The above uses conditional aggregation to get the partial counts.上面使用条件聚合来获取部分计数。 WITH ROLLUP automatically creates a totals row. WITH ROLLUP自动创建总计行。 The ISNULL() sets a status label for that row (which would otherwise display as null). ISNULL()为该行设置状态 label(否则将显示为空)。 lastly, the GROUPING() in the ORDER BY places the rollup/total after the detail rows.最后, ORDER BY中的GROUPING()将汇总/总计放在详细信息行之后。

See this db<>fiddle or this one with expanded test data.请参阅这个 db<>fiddle这个带有扩展测试数据的小提琴。

There are also ways to do this using a pivot table, but the above may be sufficient for your current needs.还有一些方法可以使用 pivot 表来执行此操作,但以上可能足以满足您当前的需求。

select  *
       ,Male + Female + Unknown as count
from    t
pivot(count(id) for gender in(Male, Female, Unknown))p
where status is not null

union all


select  'Total'
       ,count(case when gender = 'Male' then status end)    as Male
       ,count(case when gender = 'Female' then status end)  as Female
       ,count(case when gender = 'Unknown' then status end) as Unknown
       ,count(status) as Total
from   t
Status地位 Male男性 Female女性 Unknown未知 count数数
A01 A01 1 1个 0 0 0 0 1 1个
A02 A02 0 0 1 1个 0 0 1 1个
Total全部的 1 1个 1 1个 0 0 2 2个

Fiddle小提琴

You might be overthinking your requirements.你可能想多了你的要求。 Perhaps the following will work:也许以下会起作用:

SELECT Gender, Status, COUNT(*) AS count
FROM Consumer
GROUP BY Gender, Status
ORDER BY Gender, Status

It was not clear what you were expecting in your results for unknown gender.目前尚不清楚您对未知性别的结果有何期望。 If you need special handling for unknown gender and/or blank status, you can add a WHERE condition to the above for the normal cases and the UNION ALL the results with a second query to handle the special cases.如果您需要对未知性别和/或空白状态进行特殊处理,您可以在上面的正常情况下添加WHERE条件,并使用第二个查询UNION ALL结果来处理特殊情况。

SELECT Gender, Status, COUNT(*) AS count
FROM Consumer
WHERE Gender <> 'Unknown' AND Status > ''
GROUP BY Gender, Status
  UNION ALL
SELECT 'Unknown' AS Gender, '' AS Status, COUNT(*) AS count
FROM Consumer
WHERE NOT (Gender <> 'Unknown' AND Status > '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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