【发布时间】:2020-10-11 18:38:50
【问题描述】:
我有 3 个查询输出相同样式的结果。每个查询都遵循相同的结构。
查询中使用的表:
- registration_database:
包含注册参加调查但之前未注册参加调查的人的唯一 ID 号 [列:注册](与 statefileid 连接)
- statefileid:
包含 [column: 'statefileid'] 的一大群人的唯一 ID 号,并包含有关他们最喜欢的颜色是什么的信息 [column: 'favcolor']
- 2016年数据:
包含 2016 年参加调查的每个人的 statefileid 值。有一个 [列:电子],表示该人是通过电子方式“Y”还是亲自参加“N”调查(逻辑上,所有未通过电子方式参加调查的人必须亲自参加调查
- 2018年数据:
与 2016 年数据相同,但它包含 2018 年参加调查的每个人的信息
- 2020年数据:
与 2020 年数据相同,但它包含 2020 年参加调查的每个人的信息
public_scores_2020:包含一个 statefileid 列以连接到其他表和一个 [column: lrsupport] 给出从 1 到 100 的预测分数,无论它们可能支持左方向 (100) 还是支持右方向(1)
查询 1:
select
case when favcolor='r' then 'Red'
when favcolor='b' then 'Blue'
when favcolor='g' then 'Green'
when favcolor='y' then 'Yellow'
when favcolor='o' then 'Orange'
when favcolor='u' then 'Unknown Fav Color'
when favcolor='w' then 'White'
else 'Unknown Fav Color' end as "Favorite_Color"
,round(sum((lrsupport)/100),0) as "left"
,round(sum((1-(lrsupport)/100)),0) as "right"
,count(registration) AS "new_registered"
FROM registration_database
left join
statefileid
on registration=statefileid
join 2016data AS a
using (statefileid)
left join public_scores_2020
using(statefileid)
where a.electronic = 'N'
group by 1
order by 1
查询 2:
select
case when favcolor='r' then 'Red'
when favcolor='b' then 'Blue'
when favcolor='g' then 'green'
when favcolor='y' then 'yellow'
when favcolor='o' then 'orange'
when favcolor='u' then 'Unknown Fav Color'
when favcolor='w' then 'White'
else 'Unknown Fav Color' end as "Favorite_Color"
,round(sum((lrsupport)/100),0) as "left"
,round(sum((1-(lrsupport)/100)),0) as "right"
,count(registration) AS "new_registered"
FROM registration_database
left join
statefileid
on registration=statefileid
join 2018data AS a
using (statefileid)
left join public_scores_2020
using(statefileid)
where a.electronic = 'N'
group by 1
order by 1
查询 3:
select
case when favcolor='r' then 'Red'
when favcolor='b' then 'Blue'
when favcolor='g' then 'green'
when favcolor='y' then 'yellow'
when favcolor='o' then 'orange'
when favcolor='u' then 'Unknown Fav Color'
when favcolor='w' then 'White'
else 'Unknown Fav Color' end as "Favorite_Color"
,round(sum((lrsupport)/100),0) as "left"
,round(sum((1-(lrsupport)/100)),0) as "right"
,count(registration) AS "new_registered"
FROM registration_database
left join
statefileid
on registration=statefileid
join 2020data AS a
using (statefileid)
left join public_scores_2020
using(statefileid)
where a.electronic = 'N'
group by 1
order by 1
每个表查询的输出如下所示,这是针对引用 2016data 的查询
我正在尝试组合 3 个查询的输出,以便我仍然可以获得按最喜欢的颜色行排序的结果 = 红色、蓝色、绿色等 9 列:2016left、2016right、2016new_registered、2018left、2018right、2018new_registered ,2020left, 2020right, 2020new_registered。
谁能帮我解决这个问题?
【问题讨论】:
-
请您使用这个 - 请使用您的 query1 替换 qry2016 等等。
select q1.Favorite_Color, q1.left 2016left, q1.right 2016right,q1.new_registered 2016new_registered q2.left 2018left, q2.right 2018right,q2.new_registered 2018new_registered q3.left 2020left, q3.right 2020right,q3.new_registered 2020new_registered from qry2016 q1 left join qry2018 q2 on q2.Favorite_Color=q1.Favorite_Color left join qry2020 q3 on q3.Favorite_Color=q1.Favorite_Color order by 1 -
要真正解决此问题,您应该做的第一件事是不要为同一个实体设置多个表。应该只在表
data上有一个额外的年份列。 -
再进一步说,对于聚合列使用
sum和count和filter (where year = 20xx)子句。 -
@stickybit 不幸的是,我无权创建一个新表来组合 2016data、2018data 和 2020data 中的列。您对解决方法有什么想法吗?
-
@NickLal
create view years_data as select 2016 as year, * from 2016data union all select 2018 as year, * from 2018data union all select 2020 as year, * from 2020data等等,如果还有更多年份。
标签: sql postgresql join union