不完全清楚你真的想加入表吗?使用您的样本数据的理想的结果可以不使用连接:
SQL> with system(sys_id, version, state) as (
2 select 100, 'V12', 'A' from dual union all
3 select 101, 'V12', 'B' from dual union all
4 select 102, 'V12', 'A' from dual union all
5 select 103, 'V13', 'A' from dual union all
6 select 104, 'V13', 'C' from dual union all
7 select 105, 'V14', 'A' from dual
8 )
9 select version, count(*) total,
10 count(decode(state,'A',1)) stateA,
11 count(decode(state,'B',1)) stateB,
12 count(decode(state,'C',1)) stateC
13 from system
14 group by system.version
15/
VER TOTAL STATEA STATEB STATEC
--- ---------- ---------- ---------- ----------
V12 3 2 1 0
V13 2 1 0 1
V14 1 1 0 0
但如果联合表,你会得到另一种结果(不是所有SYS_ID从系统表出现在enevt表):
SQL> with system(sys_id, version, state) as (
2 select 100, 'V12', 'A' from dual union all
3 select 101, 'V12', 'B' from dual union all
4 select 102, 'V12', 'A' from dual union all
5 select 103, 'V13', 'A' from dual union all
6 select 104, 'V13', 'C' from dual union all
7 select 105, 'V14', 'A' from dual
8 ),
9 event(event_id, sys_id) as (
10 select 1, 101 from dual union all
11 select 2, 102 from dual union all
12 select 3, 103 from dual union all
13 select 4, 104 from dual union all
14 select 5, 105 from dual union all
15 select 6, 106 from dual
16 )
17 select version, count(*) total,
18 count(decode(state,'A',1)) stateA,
19 count(decode(state,'B',1)) stateB,
20 count(decode(state,'C',1)) stateC
21 from system, event
22 where system.sys_id = event.sys_id
23 group by system.version
24/
VER TOTAL STATEA STATEB STATEC
--- ---------- ---------- ---------- ----------
V12 2 1 1 0
V13 2 1 0 1
V14 1 1 0 0
这不是ID中的错字吗?
对于V12的总数是错误的,应该是3. – Emmanuel
如果您看到帖子carefuly,你会发现作者想要加入两张桌子。我指出这是一个问题。看护第一个查询。 –
这是一个示例数据。我有超过100条记录。我只知道状态值。需要通过加入'env.sys_id = st.sys_id' – NaaN