在多个表

时间:2017-12-15 10:45:38

标签: oracle count decode

我在以下结构中有三个3个表(具有相同的键):

输入表格t1:

file_in| f_in_state|
--------------------   
F01    | 1         |
F02    | 2         |
F21    | 1         |
F41    | 2         |

输入表t2:

line_in| file_in| l_in_state |     
-----------------------------
L001   | F01     | 1         |
L002   | F01     | 2         |
L003   | F01     | 2         |
L004   | F01     | 2         |
L005   | F21     | 1         |
L006   | F21     | 1         |
L007   | F21     | 1         |
L008   | F21     | 1         |

输入表格t3:

line_out|line_in| file_in| l_out_state|     
---------------------------------------
D001    |L001    | F01    | 1          |
D002    |L002    | F01    | 1          |
D003    |L003    | F01    | 1          |

我需要计算每个 "在ID" 中的每个 "文件中引用不同状态的列的出现次数然后将它们组合起来得到这样的输出:

file_in_id|file_in_state| A | B | C | D | E |
---------------------------------------------
F01       | 1           | 1 | 3 | 0 | 0 | 3 |
F02       | 2           | 2 | 0 | 0 | 0 | 0 |
F21       | 1           | 1 | 4 | 0 | 0 | 0 |
F41       | 2           | 2 | 0 | 0 | 0 | 0 | 

with:

  • A指的是具有州=' 1'
  • 的输入行数(" line_in"
  • B指的是具有州=' 2'
  • 的输入行数(" line_in"
  • C指的是具有状态=' 3'的输入行数(" line_in" )。 (在我的情况下,没有这种状态的行,但有可能发生)
  • D指的是具有州=' 1'
  • 的输出行数(" line_out"
  • E指的是具有州=' 2'
  • 的输出行数(" line_out"

所以,我尝试在查询中使用解码功能,但我没有得到希望的结果。

SELECT
    t1.file_in AS file_in_id,    
    t1.f_in_state AS file_in_state,
    COUNT(DECODE(t2.f_in_state, '1', 1, null)) AS A,
    COUNT(DECODE(t2.f_in_state, '2', 1, null)) AS B,
    COUNT(DECODE(t2.f_in_state, '3', 1, null)) AS C,
   COUNT(DECODE(t3.f_out_state, '1', 1, null)) AS D,
   COUNT(DECODE(t3.f_out_state, '2', 1, null)) AS E
FROM table1 t1,
table2 t2,
table3 t3
WHERE t1.file_in = t2.file_in (+)
AND t2.file_in = t3.file_in (+)
GROUP BY t1.file_in, t1.f_in_state
ORDER BY t1.file_in

但是,这就是我得到的:

file_in_id|file_in_state|A |B |C |D |E |
----------------------------------------
F01       |1            |1 |3 |9 |0 |12|
F02       |2            |2 |0 |0 |0 |0 |
F21       |1            |1 |4 |0 |0 |0 |
F41       |2            |2 |0 |0 |0 |0 | 

有人可以告诉我这个查询有什么问题吗?如何修复它以获得我想要的结果。

非常重要,输入表3应该是这样的:

输入表格t3:

line_out|*file_out*| file_in| l_out_state|     
---------------------------------------
D001    |W01    | F01    | 1          |
D002    |W01    | F01    | 1          |
D003    |W01    | F01    | 1          |

2 个答案:

答案 0 :(得分:0)

此查询提供了所需的结果:

select file_in, f_in_state,
       count(case l_in_state  when '1' then 1 end) a,
       count(case l_in_state  when '2' then 1 end) b,
       count(case l_in_state  when '3' then 1 end) c,
       count(case l_out_state when '1' then 1 end) d,
       count(case l_out_state when '2' then 1 end) e
  from t1 
  left join t2 using (file_in)
  left join t3 using (file_in, line_in)
  group by file_in, f_in_state
  order by file_in

如果您使用的是Oracle 11g或更高版本,也可以使用pivot

测试:

with t1(file_in, f_in_state) as (
    select 'F01', '1' from dual union all
    select 'F02', '2' from dual union all
    select 'F21', '1' from dual union all
    select 'F41', '2' from dual ),
t2(line_in, file_in, l_in_state) as (
    select 'L001', 'F01', '1' from dual union all
    select 'L002', 'F01', '2' from dual union all
    select 'L003', 'F01', '2' from dual union all
    select 'L004', 'F01', '2' from dual union all
    select 'L005', 'F21', '1' from dual union all
    select 'L006', 'F21', '1' from dual union all
    select 'L007', 'F21', '1' from dual union all
    select 'L008', 'F21', '1' from dual ), 
t3(line_out, line_in, file_in, l_out_state) as (
    select 'D001', 'L001', 'F01', '1' from dual union all
    select 'D002', 'L002', 'F01', '1' from dual union all
    select 'D003', 'L003', 'F01', '1' from dual )
select file_in, f_in_state,
       count(case l_in_state  when '1' then 1 end) a,
       count(case l_in_state  when '2' then 1 end) b,
       count(case l_in_state  when '3' then 1 end) c,
       count(case l_out_state when '1' then 1 end) d,
       count(case l_out_state when '2' then 1 end) e
  from t1 
  left join t2 using (file_in)
  left join t3 using (file_in, line_in)
  group by file_in, f_in_state
  order by file_in

输出:

FILE_IN F_IN_STATE          A          B          C          D          E
------- ---------- ---------- ---------- ---------- ---------- ----------
F01     1                   1          3          0          3          0
F02     2                   0          0          0          0          0
F21     1                   4          0          0          0          0
F41     2                   0          0          0          0          0

答案 1 :(得分:0)

变体与SUM

Select a.file_in, a.f_in_state,
Sum(Case When b.l_in_state=1 Then 1 Else 0 End) A,
Sum(Case When b.l_in_state=2 Then 1 Else 0 End) B,
Sum(Case When b.l_in_state=3 Then 1 Else 0 End) C,
Sum(Case When c.l_out_state=1 Then 1 Else 0 End) D,
Sum(Case When c.l_out_state=2 Then 1 Else 0 End) E
From T1 a
Left join T2 b on a.file_in=b.file_in
Left join T3 c on a.file_in=c.file_in and b.line_in=c.line_in
GROUP BY a.file_in, a.f_in_state
ORDER BY a.file_in