我正在努力解决部分完成的同事给我的查询。我得到的错误是“不是单组分组函数”,我尝试了各种分组依据语句,似乎都没有一个能解决我的问题
“未调整的其他”和“其他调整的”似乎是select语句中仅有的非聚合部分,但是当我尝试按那些分组时,似乎不起作用。
我还没有使用串联聚合字段的经验,因此我处于新领域,还无法对其进行故障排除。
SELECT CASE WHEN sum(unadj_gross_for_other_brand+adjustments_for_other_brand) = 0 AND
sum(adjustments_for_other_brand+redist_gross) = 0 THEN 0 ELSE 1 END status,
'Unadjusted Other='||unadj_gross_for_other_brand||
' Adjustments Other='||adjustments_for_other_brand||
' Adjusted Other='||sum(unadj_gross_for_other_brand+adjustments_for_other_brand) ||
' Redistributed='||sum(redist_gross) Note
FROM
(
select
1 as unadj_gross_for_other_brand,
0 as adjustments_for_other_brand,
0 as redist_gross
from dual
UNION
select
0 as unadj_gross_for_other_brand,
2 as adjustments_for_other_brand,
0 as redist_gross
from dual
UNION
select
0 as unadj_gross_for_other_brand,
0 as adjustments_for_other_brand,
3 as redist_gross
from dual
);
预期结果如下:
Status Note
1 Unadjusted Other = 1, Adjusted Other = 2, Adjusted Other = 3, Redistributed = 3
或
Status Note
0 Unadjusted Other = 0, Adjusted Other = 0, Adjusted Other = 0, Redistributed = 0
答案 0 :(得分:1)
您有对相同值的汇总和非汇总引用的混合。串联不是真的相关;如果将所有“注释”值都作为单独的列,则会遇到相同的问题。
由于只需要一行输出,因此只需将“注释”列中的前两个引用相加即可,
'Unadjusted Other='||unadj_gross_for_other_brand||
' Adjustments Other='||adjustments_for_other_brand||
到
'Unadjusted Other='||sum(unadj_gross_for_other_brand)||
----------------------------^^^^ ^
' Adjustments Other='||sum(adjustments_for_other_brand)||
------------------------------^^^^ ^
或原位:
SELECT CASE WHEN sum(unadj_gross_for_other_brand+adjustments_for_other_brand) = 0 AND
sum(adjustments_for_other_brand+redist_gross) = 0 THEN 0 ELSE 1 END status,
'Unadjusted Other='||sum(unadj_gross_for_other_brand)||
' Adjustments Other='||sum(adjustments_for_other_brand)||
' Adjusted Other='||sum(unadj_gross_for_other_brand+adjustments_for_other_brand) ||
' Redistributed='||sum(redist_gross) Note
FROM (
...
);
STATUS NOTE
---------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 Unadjusted Other=1 Adjustments Other=2 Adjusted Other=3 Redistributed=3
结果中的所有非文字值现在都是合计的,因此您不需要group-by子句。
db<>fiddle,其中两个版本的状态为零。
答案 1 :(得分:1)
以下应该可以工作
SELECT CASE WHEN sum(unadj_gross_for_other_brand+adjustments_for_other_brand) = 0
AND sum(adjustments_for_other_brand+redist_gross) = 0 THEN 0 ELSE 1
END status
,'Unadjusted Other='||sum(unadj_gross_for_other_brand)
||' Adjustments Other='||sum(adjustments_for_other_brand)
||' Adjusted Other='||sum(unadj_gross_for_other_brand+adjustments_for_other_brand)
||' Redistributed='||sum(redist_gross) as Note
FROM (
select
1 as unadj_gross_for_other_brand,
0 as adjustments_for_other_brand,
0 as redist_gross
from dual
UNION
select
0 as unadj_gross_for_other_brand,
2 as adjustments_for_other_brand,
0 as redist_gross
from dual
UNION
select
0 as unadj_gross_for_other_brand,
0 as adjustments_for_other_brand,
3 as redist_gross
from dual
);