Sql 从两个不同的表中查找计数和字段
我有以下两个表格:Sql 从两个不同的表中查找计数和字段,sql,oracle,Sql,Oracle,我有以下两个表格: Table A: -------- Id Service 101 S1 101 S1 101 S2 101 S2 102 S4 Table B: -------- Service Status S1 Active S1 Active S2 Inactive S2 Inactive S4 Cancelled Result Required:
Table A:
--------
Id Service
101 S1
101 S1
101 S2
101 S2
102 S4
Table B:
--------
Service Status
S1 Active
S1 Active
S2 Inactive
S2 Inactive
S4 Cancelled
Result Required:
----------------
Id Service Count Status
101 S1 2 Active
101 S2 2 Inactive
102 S4 1 Cancelled
我们如何编写一个sql查询来根据所需的结果从这两个表中获取数据
非常感谢你的帮助
编辑:
是否有一种方法可以让我们使用同一个sql查询同时填充表的不同“计数列”。例如,如果结果是以以下形式要求的:
Id Service Active_Count Inactive_Count Cancelled_Count
---------------------------------------------------------------------
101 S1 2 0 2
101 S1 0 2 0
102 S4 0 0 1
试试这个:
select id, A.service, count(1), status
from A inner join B on A.service = B.service
group by id, A.service, status
您的尝试和遇到的问题是什么?请输入您的代码。建议不要使用逗号分隔的连接。它被弃用了@据我所知,隐式联接并没有被弃用。你能提供一个来源吗?@FuzzyTree:-你可以在这里检查:-(可能是我应该使用一个坏习惯,而不是不推荐的)
select id, A.service, count(1), status
from A inner join B on A.service = B.service
group by id, A.service, status