向表中添加临时行

时间:2015-06-29 12:40:43

标签: sql oracle

我制定了这个SQL语句......

  select 
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table1
    UNION ALL
    select 
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table2
    UNION ALL
    select 
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table3

...它给了我以下输出:

blabla          blablabla         blablalasba

10              23                454545
123             2323              445445
123             212               121221

我想添加一个包含一些文字的额外列。

我的最终结果应该是:

             blabla          blablabla         blablalasba

text1        10              23                454545
text2        123             2323              445445
text3        123             212               121221

2 个答案:

答案 0 :(得分:2)

您需要在UNION的每个部分添加文本:

  select 
        'text1' AS ColumnTitle
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table1
    UNION ALL
    select 
        'text2' AS ColumnTitle
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table2
    UNION ALL
    select 
        'text3' AS ColumnTitle
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table3

答案 1 :(得分:2)

那些是“列”而非“行”。您可以添加如下的静态文本:

 select 'text1' text,
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table1
    UNION ALL
    select 'text2' text,
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table2
    UNION ALL
    select 'text3' text,
        SUM(CASE WHEN "XY" LIKE '%XP Pro%' THEN 1 ELSE 0 END) blabla,        
        SUM(CASE WHEN "XYZ" = 1 THEN 1 ELSE 0 END) blablabla,
        SUM(CASE WHEN "ABS" = 1 THEN 1 ELSE 0 END) blablalasba        
    from 
    table3