以列而不是行显示union语句

时间:2016-02-23 15:56:56

标签: sql oracle

我需要在关于属性'Label'值的不同列中拆分union语句的结果,我不能使用'JOIN'语句,因为我需要获取数据和计数,对于所有情况,不仅仅是对于案例连接条件满足的地方。

我有这样的查询:

$cred.GetNetworkCredential().Password

所以我得到的结果如下:

Select 
    'Label_1' as Label,
    ATTR_1, ATTR2, ATTR3,
    COUNT (AATR1)
from 
    table
where 
    ATTR4 = '10'
GROUP by  
    'Label_1' as Label, ATTR_1, ATTR2, ATTR3

UNION ALL 

Select 
    'Label_2' as Label,
    ATTR_1, ATTR2, ATTR3,
    COUNT (AATR1)
from 
    table
where 
    ATTR4 = '20'
GROUP by  
    'Label_2' as Label, ATTR_1, ATTR2, ATTR3

我想得到:

Label   | ATTR_1| ATTR2| ATTR3| COUNT (AATR1)|
-------------------------------------------
Label_1 | xxxxx  |xxxxxx|xxxxxx|xxxxxxx      |
Label_2 |yyyyyy  |yyyyyy|yyyyyy|yyyyyyy      |

3 个答案:

答案 0 :(得分:1)

如果您有两个结果集,每个结果集各有一行,您只需执行CROSS JOIN并排设置它们,就像这样:

select 
    Label_1, ATTR_1_1, ATTR_2_1, ATTR_3_1, AATR1_Count_1, 
    Label_2, ATTR_1_2, ATTR_2_2, ATTR_3_2, AATR1_Count_2
from (
    Select 'Label_1' as Label_1,
           ATTR_1 as ATTR_1_1,
           ATTR2 as ATTR2_1,
           ATTR3 as ATTR3_1,
           COUNT(AATR1) as AATR1_Count_1
    from table
    where ATTR4='10'
    GROUP by ATTR_1, ATTR2, ATTR3
) t1
cross join (
    Select 'Label_2' as Label_2,
           ATTR_1 as ATTR_1_2,
           ATTR2 as ATTR2_2,
           ATTR3 as ATTR3_2,
           COUNT(AATR1) as AATR1_Count_2
    from table
    where ATTR4='20'
    GROUP by ATTR_1, ATTR2, ATTR3
) t2

将行转换为列的另一个选项(如果交叉连接不合适)是执行数据透视查询。谷歌搜索“Oracle数据库查询示例”,你会看到很多策略。

答案 1 :(得分:0)

尝试

SELECT Label, n, ATTR_1, ATTR2, ATTR3, COUNT(ATTR1) FROM(
    Select 'Label_1' as Label,
            row_number() OVER (ORDER BY ATTR_1, ATTR2, ATTR3) n,
            ATTR_1,
            ATTR2,
            ATTR3,
            COUNT (AATR1)
     from table
     where ATTR4='10'
     GROUP by  'Label_1' as Label, ATTR_1, ATTR2, ATTR3
) As firstTable

FULL OUTER JOIN

SELECT Label, m, ATTR_1, ATTR2, ATTR3, COUNT(ATTR1) FROM(
     Select 'Label_2' as Label,
            row_number() OVER (ORDER BY ATTR_1, ATTR2, ATTR3) m,
            ATTR_1,
            ATTR2,
            ATTR3,
            COUNT (AATR1)
     from table
     where ATTR4='20'
     GROUP by  'Label_1' as Label, ATTR_1, ATTR2, ATTR3
) AS secondTable

ON firstTable.n = secondTable.m

这将为每一行提供一个数字,然后加入数字,因此无论你有多少行都可以使用。

答案 2 :(得分:-1)

这不会给你准确的计数吗?

select attr_1,
       attr2,
       attr3,
       nvl(count(case when attr4 = '10' then attr_1 end), 0) label_1_cnt,
       nvl(count(case when attr4 = '20' then attr_1 end), 0) label_2_cnt
from   table1
where  attr4 in ('10', '20')
group by attr_1, attr2, attr3;

另外,无需重复每行中的attr_1 / 2/3列。此外,它的优点是只能查询一次表,而不是原始查询的两倍。

以下是上述查询将使用一些示例数据生成的内容的演示:

with t1 as (select 1 attr_1, 2 attr2, 3 attr3, 10 attr4 from dual union all
            select 1 attr_1, 2 attr2, 3 attr3, 10 attr4 from dual union all
            select 2 attr_1, 3 attr2, 4 attr3, 10 attr4 from dual union all
            select 1 attr_1, 2 attr2, 3 attr3, 20 attr4 from dual union all
            select 2 attr_1, 3 attr2, 4 attr3, 20 attr4 from dual union all
            select 2 attr_1, 3 attr2, 4 attr3, 20 attr4 from dual union all
            select 3 attr_1, 4 attr2, 5 attr3, 20 attr4 from dual union all
            select 2 attr_1, 3 attr2, 4 attr3, 30 attr4 from dual)
select attr_1,
       attr2,
       attr3,
       nvl(count(case when attr4 = '10' then attr_1 end), 0) label_1_cnt,
       nvl(count(case when attr4 = '20' then attr_1 end), 0) label_2_cnt
from   t1
where  attr4 in ('10', '20')
group by attr_1, attr2, attr3
order by 1, 2, 3;

    ATTR_1      ATTR2      ATTR3 LABEL_1_CNT LABEL_2_CNT
---------- ---------- ---------- ----------- -----------
         1          2          3           2           1
         2          3          4           1           2
         3          4          5           0           1

您的原始查询将使用相同的示例数据生成以下结果:

LABEL       ATTR_1      ATTR2      ATTR3 COUNT(ATTR_1)
------- ---------- ---------- ---------- -------------
Label_1          1          2          3             2
Label_1          2          3          4             1
Label_2          1          2          3             1
Label_2          3          4          5             1
Label_2          2          3          4             2