sql - 如何将两个以上的选择查询合并到oracle中的单个结果集
问题描述
我需要将四个查询的结果集合并到单个结果输出中。
例子:
Select sum(marks) total from table1 Where id > 1000 and id < 2000
Select sum(marks) total1 from table1 where id =>2000
Select sum(tables) totaltbl from table2 where id > 1000 and id < 2000
Select sum(tables) totaltbl1 from table2 where id => 2000
我需要以下格式的输出
Total total1 totaltbl totaltbl1
100. 2000. 10. 30
解决方案
一种选择是在加入表期间应用条件聚合:
SELECT SUM(CASE WHEN t1.id > 1000 AND t1.id < 2000 THEN t1.marks END ) "Total",
SUM(CASE WHEN t1.id >= 2000 THEN t1.marks END ) "Total1",
SUM(CASE WHEN t2.id > 1000 AND t2.id < 2000 THEN t2.tables END ) "Totaltbl",
SUM(CASE WHEN t2.id >= 2000 THEN t2.tables END ) "Totaltbl1"
FROM table1 t1
CROSS JOIN table2 t2
推荐阅读
- c - tcpi_rcv_mss 是什么意思?
- flutter - Bloc:是否有可能产生 2 次相同的状态?
- webrtc - android问题中的多个对等连接Web-RTC
- python - 有没有办法自动加入来自同一组的具有相同值的列表?
- ibm-mq - IBM MQ SDK 取决于客户端
- java - 从不同的 Activity 返回到特定的 Fragment
- javascript - 将 textarea 的内容推送到数组中
- python-3.x - ImportError:请从 https://www.github.com/nvidia/apex 安装 apex 以使用分布式和 fp16 训练
- python - 扩展具有附加属性的类时出现 TypeError
- ios - 在 tvOS 上播放时仅出现“MPMediaItemPropertyTitle”,尽管所有其他信息都在控制中心