按(分段)对数据进行分组,然后在Oracle中将两个表一起加入

时间:2017-08-06 09:39:11

标签: sql oracle plsql

有两个表许可组织

许可证表包含5列

oracle_apps       tableau      sab_bi      tririga     time_snapshot
0                  1            1           1          2017-06-13 08:12:02.640
0                  0            0           1          2017-06-13 09:12:02.640
0                  0            1           0          2017-06-13 11:52:02.640
0                  1            0           1          2017-06-14 09:12:02.640   
0                  0            1           0          2017-06-14 10:12:02.640

组织表有2列

license_name                 license_count
oracle_                      5.0000000
tableau_                     1.0000000
sab_                         20.0000000 
tririga_                     10.0000000 

因此,输出将是一个选择查询或pl SQL存储过程,它将包含两个表的连接,它将显示一天中使用的许可证数量(计数)

输出:

 oracle_apps    tableau    sab_bi    tririga    time_snapshot  oracle_    tableau_    sap_        tririga_  

 0              1          2         2          2017-06-13     5.0000000  1.0000000   20.0000000  10.0000000
 0              1          1         1          2017-06-14     5.0000000  1.0000000   20.0000000  10.0000000  

对于Ex: 2017-06-13 使用总 5 许可证,并且单独使用 0,1,2,2 即sum(oracle_apps),sum(tableau),sum(sab_bi),sum(tririga) 和lly,在2017-06-14使用总共3个许可证,即(0,1,1,1)

通过to_char(time_snapshot,yyyy-mm-dd)使用分组这件事很容易,但我无法加入表并使用行作为一个列,因为在组织表中,行将在输出中列,我知道使用 PIVOT 的一种方法,但我很困惑如何在结果和每一行中得到它 license_name 值将相同,即(5.0000000 | 1.0000000 | 20.0000000
 | 10.0000000)

是否有对数据进行分组并将表格合并在一起的解决方案?

2 个答案:

答案 0 :(得分:2)

使用CROSS JOINSUMMAXCASE..WHEN..THEN..GROUP BY

SELECT trunc( l.time_snapshot) As time_snapshot,
       sum( l.oracle_apps ) As oracle_apps,
       sum( l.tableau ) As tableau,
       sum( l.sab_bi  ) As sab_bi,
       sum( l.tririga ) As tririga,
       max( CASE WHEN o.license_name = 'oracle_' THEN o.license_count END ) As oracle_,
       max( CASE WHEN o.license_name = 'tableau_' THEN o.license_count END ) As tableau_,
       max( CASE WHEN o.license_name = 'sab_' THEN o.license_count END ) As sab_,
       max( CASE WHEN o.license_name = 'tririga_' THEN o.license_count END ) As tririga_
FROM licenses l
CROSS JOIN organization o 
GROUP BY trunc( l.time_snapshot)

演示:http://sqlfiddle.com/#!4/24b53/5

答案 1 :(得分:0)

我建议您在加入之前转动数据

select l.dte, l.oracle_apps, l.tableau, l.sab_bi, l.tririga,,
       o.oracle_, o.tableau_, o.sab_, o.tririga_
from (select trunc(l.time_snapshot) as dte,
             sum(l.oracle_apps) as oracle_apps,
             sum(l.tableau) as tableau,
             sum(l.sab_bi) as sab_bi,
             sum(l.tririga) as tririga
      from licenses l
      group by trunc(l.time_snapshot)
     ) l cross join
     (select sum(case when license_name = 'oracle_' then license_count else 0 end) as oracle_,
             sum(case when license_name = 'tableau_' then license_count else 0 end) as tableau_,
             sum(case when license_name = 'sab_' then license_count else 0 end) as sab_,
             sum(case when license_name = 'tririga_' then license_count else 0 end) as tririga_        
      from organization o
     ) o;

对单个表进行聚合对我来说似乎更安全(如果oracle_在组织表中出现两次,那么许可计数将会关闭)。此外,生成的查询可能更快,因为单个表上的优化通常比在多个表上更容易。