2014-03-27 27 views
1

我有两个表计数和组由多个coulmns

SYSTEM 
sys_id version state 
(PK) 
------------------------- 
100  V12  A 
101  V12  B 
102  V12  A 
103  V13  A 
104  V13  C 
105  V14  A 

ENVNT 
envnt_id sys_id 
      (FK) 
---------------- 
1   101 
2   102 
3   103 
4   104 
5   105 
6   106 

需要加入这个表来得到下面的结果,

   total 
version  sys_ids state A state B state C 
---------------------------------------------------- 
V12    3   2  1  0 
V13    2   1  0  1 
V14    1   1  0  0 

我已经开始喜欢,

SELECT st.version as version, count(st.version) as total sys_ids 
FROM SYSTEM st 
,ENVNT env 
where env.sys_id = st.sys_id group by st.sys_id; 

但我对如何进一步暗示这一逻辑毫无头绪。

回答

2

尝试此查询:

select st.version as version, count(st.version) as total sys_ids,count(a),count(b),count(c) from 
(
    SELECT sys_id,version,state, 
    MAX(DECODE(state,'A',state)) as a, 
    MAX(DECODE(state,'B',state)) as b, 
    MAX(DECODE(state,'C',state)) as c 
    FROM SYSTEM st 
    group by sys_id,version,state 
) group by version; 

我没有包括查询中的其他表,因为根据您的数据和所需的输出e不使用其他表格。如果您需要其他表格中的数据,您可以尝试使用加入,就像您在查询中显示的那样。

0

不完全清楚你真的想加入表吗?使用您的样本数据的理想的结果可以不使用连接:

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中的错字吗?

+0

对于V12的总数是错误的,应该是3. – Emmanuel

+0

如果您看到帖子carefuly,你会发现作者想要加入两张桌子。我指出这是一个问题。看护第一个查询。 –

+0

这是一个示例数据。我有超过100条记录。我只知道状态值。需要通过加入'env.sys_id = st.sys_id' – NaaN

0

您只能使用SYSTEM表和PIVOT条款:

with SYSTEM(sys_id, version, state) as 
(
    select 100, 'V12', 'A' from dual 
    union all 
    select 101, 'V12', 'B' from dual 
    union all 
    select 102, 'V12', 'A' from dual 
    union all 
    select 103, 'V13', 'A' from dual 
    union all 
    select 104, 'V13', 'C' from dual 
    union all 
    select 105, 'V14', 'A' from dual 
) 
SELECT version, state_a + state_b + state_c total_sys_ids, state_a, state_b, state_c 
FROM 
(
    select * from SYSTEM st 
) 
pivot (count(sys_id) for state in ('A' as state_a, 'B' as state_b, 'C' as state_c)); 

这给了预期的结果:

VERSION TOTAL_SYS_IDS STATE_A STATE_B STATE_C 
1 V12  3   2   1  0 
2 V13  2   1   0  1 
3 V14  1   1   0  0 
+0

作者最初想要连接表。 –

+0

这是一个示例数据。我有超过100条记录。我只知道'state'的值。需要通过加入env.sys_id = st.sys_id' – NaaN

+0

来获得其他值。对不起,但我不明白为什么你需要在这里加入:你只使用'version'和'SYSTEM'表中的'state'。 – Emmanuel