如何将3个查询的结果合并到一个表中

问题描述

我有3个查询,它们输出相同样式的结果。每个查询都遵循相同的结构。

查询中使用的表:

  • registration_database:

包含先前未注册参加调查的人的唯一ID号[列:注册](与statefileid一起加入)

  • statefileid:

为[列:'statefileid']的一大群人提供唯一的ID号,并具有有关他们最喜欢的颜色是[列:'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",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",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的查询

The output for each table query looks like this,this is for the query that referenced 2016data

我正在尝试合并3个查询输出,以便仍然可以按9列按喜欢的颜色行=红色,蓝色,绿色等获得结果:2016left,2016right,2016new_registered,2018left,2018right,2018new_registered ,2020left,2020right,2020new_registered。

有人可以帮我吗?

解决方法

使用通用表表达式组合按年划分的表,这将降低查询的复杂性。这只是一个例子,如果不做进一步的努力,它可能无法工作:

with all_data as (
        select statefileid,lrsupport,electronic,2016 as Year
        from 2016data
        union all
        select statefileid,2018 as Year  
        from 2018data
        union all
        select statefileid,2020 as Year
        from 2020data
        )

SELECT
  CASE
               when stf.favcolor='r' then 'Red'
               when stf.favcolor='b' then 'Blue'
               when stf.favcolor='g' then 'green'
               when stf.favcolor='y' then 'yellow'
               when stf.favcolor='o' then 'orange'
               when stf.favcolor='u' then 'Unknown Fav Color'
               when stf.favcolor='w' then 'White'
               else 'Unknown Fav Color'
  end as "Favorite_Color",count(case when a.year = 2016 then reg.registration end) AS "new_registered_2016",count(case when a.year = 2018 then reg.registration end) AS "new_registered_2018",count(case when a.year = 2020 then reg.registration end) AS "new_registered_2020"
FROM registration_database AS reg
INNER JOIN statefileid AS stf ON reg.registration = stf.statefileid
INNER JOIN all_data AS a ON stf.statefileid = a.statefileid
LEFT JOIN public_scores_2020 AS score ON stf.statefileid = score.statefileid
WHERE a.electronic = 'N'
GROUP BY 1
ORDER BY 1

在整个查询中引用列时,也请使用表名或别名,我还建议使用更明确的联接来帮助实现这一点。我认为对表statefileid的联接可能是内部联接