[英]Display the total number of each catagory in each column seperately -- SQL
Where is a database like数据库在哪里
gender![]() |
ssc_b ![]() |
---|---|
F ![]() |
Central![]() |
F ![]() |
Central![]() |
F ![]() |
Other![]() |
M![]() |
Central![]() |
M![]() |
Other![]() |
I used the count and group by command but it shows:我按命令使用了计数和分组,但它显示:
gender![]() |
num_gender ![]() |
ssc_b ![]() |
num_ssc_b ![]() |
---|---|---|---|
F ![]() |
2 ![]() |
Central![]() |
2 ![]() |
F ![]() |
1 ![]() |
Other![]() |
1 ![]() |
M![]() |
1 ![]() |
Central![]() |
1 ![]() |
M![]() |
1 ![]() |
Other![]() |
1 ![]() |
I want the display the total number of each catagory in each column seperately, like我想分别显示每列中每个类别的总数,比如
gender![]() |
num_gender ![]() |
ssc_b ![]() |
num_ssc_b ![]() |
---|---|---|---|
F ![]() |
3 ![]() |
Central![]() |
3 ![]() |
M![]() |
2 ![]() |
Other![]() |
2 ![]() |
SELECT gender as key, count(*) as value
FROM <table>
GROUP BY gender
UNION ALL
SELECT ssc_b as key, count(*) as value
FROM <table>
GROUP BY ssc_b
Exactly for what you asked, the answer is正是针对你所问的,答案是
select a.gender, a.s, b.gender, b.s from
(select ROW_NUMBER() OVER() AS num_row, gender, sum(num_gender) s from t group by gender) a
outer join
(select ROW_NUMBER() OVER() AS num_row, ssc_b, sum(num_ssc_b) s from t group by ssc_b) b
on a.num_row=b.num_row
But maybe more logical would be to have the inner queries above as two separate queries.但也许更合乎逻辑的是将上面的内部查询作为两个单独的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.