Oracle / SQL - 合并来自“不相关”的无关表的计数 [英] Oracle/SQL - Combining counts from 'unrelated' unrelated tables

查看:148
本文介绍了Oracle / SQL - 合并来自“不相关”的无关表的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我存储了用于生成窗口小部件的记录的两个表 GOOD BAD 。我的表格如下

Let's assume I two tables GOOD and BAD that stores records for widget production. My tables look like this

Widget      Good
----------------
Widget A    Y
Widget A    Y
Widget B    Y

Widget      Bad
----------------
Widget A    Y
Widget B    Y

我有这两个基本查询

select count(*) as good from table_good where widget = 'Widget A' and Good = 'Y'
select count(*) as bad from table_bad where widget = 'Widget A' and Bad = 'Y'

在这样的两个表中

good
----
2

bad
---
1

单个查询,其中我将返回一个具有单个记录的表格,如下所示

I would like to combine these into a single query where I would get back a table with a single record that looks like this

good    bad
-----------
2       1

做这个。我认为做一个联合和在其他表中设置假列选择会做到,但我得到了正确的表模式,但有两个单独的记录。

Can someone point me how to do this. I thought doing a union and setting up fake columns in the other tables selects would do it, but I got the right table schema back, but had two seperate records.

谢谢!

推荐答案

尝试:

SELECT (SELECT COUNT(*) AS good 
        FROM   table_good 
        WHERE  widget = 'Widget A' 
               AND good = 'Y') AS good, 
       (SELECT COUNT(*) AS bad 
        FROM   table_bad 
        WHERE  widget = 'Widget A' 
               AND bad = 'Y')  AS bad 
FROM   dual  

这篇关于Oracle / SQL - 合并来自“不相关”的无关表的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆