简体   繁体   English

分别显示每一列每一类的总数——SQL

[英]Display the total number of each catagory in each column seperately -- SQL

Where is a database like数据库在哪里

gender性别 ssc_b ssc_b
F F Central中央
F F Central中央
F F Other其他
M Central中央
M Other其他

I used the count and group by command but it shows:我按命令使用了计数和分组,但它显示:

gender性别 num_gender num_gender ssc_b ssc_b num_ssc_b num_ssc_b
F F 2 2个 Central中央 2 2个
F F 1 1个 Other其他 1 1个
M 1 1个 Central中央 1 1个
M 1 1个 Other其他 1 1个

I want the display the total number of each catagory in each column seperately, like我想分别显示每列中每个类别的总数,比如

gender性别 num_gender num_gender ssc_b ssc_b num_ssc_b num_ssc_b
F F 3 3个 Central中央 3 3个
M 2 2个 Other其他 2 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.

相关问题 SQL | 根据每列的日期值进行计数 - SQL | Count based on each column's date value SQL 如何查找一年中每天 2 个不同组的平均出行次数 - SQL How to find the AVG number of trips taken by 2 different groups for each day in a year 将字符串解析为元素,其中每个元素将获得具有唯一名称的列,Bigquery sql - Parse string into elements, where each element will get column with unique name, Bigquery sql 对于红移 sql 列中的每个唯一项,根据查找/扫描 window 获取最后一行 - For each unique item in a redshift sql column, get the last rows based on a looking/scanning window 将同一组中的每个列值移动到新列 SQL Big Query - Move each column value from the same group to new column SQL Big Query SQL 计算总和小计数但唯一 - SQL to calculate the overall total and sub total number but unique SQL:如何添加虚拟列(每个单元格包含“A”或 integer 或其他字符串 - SQL : How to add a dummy column (with each cell containing "A", or an integer or some other string 如何从每个具有特定条件的两个表中计算总交叉 - How to count total crossover from two tables each with specific conditions 从另一个表中为每个 ID 写入数组中的每一列获取值 - Fetch values from another table for each column written in an array for each ID 计算分隔字符串中每个值的实例数 - Count the number of instances for each value in a delimited string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM