【发布时间】:2020-09-18 04:02:57
【问题描述】:
目前我有两个 sql 查询,每个查询都返回特定日期特定列的总数:
select first_name COUNT(*) as totalCount from some_table
where the_date >= '2020-09-14' and the_date <='2020-09-15'
group by first_name
select first_name COUNT(*) as totalCount from some_table
where the_date >= '2020-09-16' and the_date <='2020-09-17'
group by first_name
每个都会返回一个表格,例如:
first_name | totalCount
相反,我想要一个返回结果的查询:
first_name | count1 | count2
count1 是第一个查询的总计数,count2 是第二个查询的总计数
我试过了:
select first_name COUNT(*) as count1 from some_table
where the_date >= '2020-09-14' and the_date <='2020-09-15'
group by first_name
UNION
select first_name COUNT(*) as count2 from some_table
where the_date >= '2020-09-16' and the_date <='2020-09-17'
group by first_name
但它仍然只返回count1的结果。
【问题讨论】:
-
你试过UNION吗?
-
@Mech 我做了,让我添加我尝试过的内容
标签: sql postgresql