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

如何解决如何将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的联接可能是内部联接

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-