[英]Join two queries on the exact same column into one result table
INSERT INTO COLOR
SELECT 'COLOR', 'RED'
UNION
SELECT 'config', 'RED'
UNION
SELECT 'CONFIG_VALUE', 'RED'
UNION
SELECT 'XYZ', 'BLUE'
UNION
SELECT 'TEST', 'BLUE'
UNION
SELECT 'COLOR', 'BLUE'
UNION
SELECT 'COLOR', 'RED'
UNION
SELECT 'COLOR', 'BLUE'
UNION
SELECT 'COLOR', 'RED'
I would like to run two queries on the same column, to get a result table that has a count of how many blue and red items there are - blue as the title for one column, and red being the title of the second column.我想在同一列上运行两个查询,以获得一个结果表,其中计算有多少蓝色和红色项目 - 蓝色作为一列的标题,红色是第二列的标题。
I have the two separate queries returning the count for each one, but when I join these with a union it turns out strange.我有两个单独的查询返回每个查询的计数,但是当我将这些与联合加入时,结果很奇怪。
Is this possible?这可能吗? Thanks.
谢谢。
You can use conditional aggregation:您可以使用条件聚合:
select sum(case when color = 'BLUE' then 1 else 0 end) as blue,
sum(case when color = 'RED' then 1 else 0 end) as red
from t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.