[英]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 ![]() |
Gender![]() |
Status![]() |
---|---|---|
1 ![]() |
Male![]() |
A01 ![]() |
2 ![]() |
Male![]() |
|
3 ![]() |
Female![]() |
A02 ![]() |
4 ![]() |
Female![]() |
|
5 ![]() |
Unknown![]() |
|
6 ![]() |
Male![]() |
|
7 ![]() |
Female![]() |
|
8 ![]() |
Unknown![]() |
I want to display我要展示
Gender![]() |
Status![]() |
Count![]() |
---|---|---|
Male![]() |
A01 ![]() |
1 ![]() |
Female![]() |
A02 ![]() |
1 ![]() |
Unknown![]() |
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 ![]() |
1 ![]() |
0 ![]() |
0 ![]() |
1 ![]() |
A02 ![]() |
0 ![]() |
1 ![]() |
0 ![]() |
1 ![]() |
Total![]() |
1 ![]() |
1 ![]() |
0 ![]() |
2 ![]() |
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.