Oracle - 通过创建重复行进行分组

时间:2017-01-26 15:46:00

标签: sql oracle group-by alias

我有一个如下所示的查询:

select nvl(trim(a.code), 'Blanks') as Ward, count(b.apcasekey) as UNSP,     count(c.apcasekey) as GRAPH,
count(d.apcasekey) as "ANI/PIG", 
(count(b.apcasekey) +  count(c.apcasekey) + count(d.apcasekey)) as "TOTAL   ACTIVE", 
count(a.apcasekey) as "TOTAL OPEN" from (etc...)

group by a.code
order by Ward

nvl(trim(a.code), 'Blanks') as Ward的原因是有时a.code是一个空字符串,有时它是null

问题在于,当我使用Group By语句时,我无法使用Ward或我收到错误

  

Ward:标识符无效

我只能使用a.code所以我为'Blanks'获得了2行,如下所示

1   Blanks  7    0   0  7   7
2   Blanks  23   1   1  25  30
3   W01     75   4   0  79  91
4   W02     62   1   0  63  72
5   W03     140  2   0  142 162
6   W04     6    1   0  7   7
7   W05     46   0   1  47  48
8   W06     322  46  1  369 425
9   W07     91   0   1  92  108
10  W08     93   2   0  95  104
11  W09     28   1   0  29  30
12  W10     25   0   0  25  28

我需要的是将'Blanks'行合并为一行。帮助不大?

感谢。

1 个答案:

答案 0 :(得分:4)

您不能在(?:)中使用别名,但可以使用构建值的表达式:

GROUP BY