我使用以下代码按类型在表中计算记录:
select null link, CASE when SUBSTR(PRJA_TYPE,1,3) ='CHO' then 'CHO'
else PRJA_TYPE
END as label,
COUNT(PRJA_TYPE) Contract_Type
from "SPM"."PROJECT_AGREEMENTS"
where PRJA_ACTIVE = 1 and PRJA_TYPE IS NOT NULL
group by PRJA_TYPE
order by PRJA_TYPE
它给了我以下结果:
LINK LABEL CONTRACT_TYPE
- Ballpark 14
- CHO 7
- CHO 7
- CHO 6
- CHO 30
- CHO 11
- CHO 9
- CHO 1
- CHO 2
- LOA 9
这部分按预期工作。然后,我需要将Label列中标记为CHO的项目进行汇总。所以我的最终结果将是这样的:
LINK LABEL CONTRACT_TYPE
- Ballpark 14
- CHO 73
- LOA 9
非常感谢任何帮助。
答案 0 :(得分:1)
您需要通过表达式汇总:
select null as link,
(case when SUBSTR(PRJA_TYPE, 1, 3) = 'CHO' then 'CHO'
else PRJA_TYPE
end) as label,
COUNT(*) Contract_Type
from "SPM"."PROJECT_AGREEMENTS"
where PRJA_ACTIVE = 1 and PRJA_TYPE IS NOT NULL
group by (case when SUBSTR(PRJA_TYPE, 1, 3) = 'CHO' then 'CHO'
else PRJA_TYPE
end)
order by label;
答案 1 :(得分:0)
cte我会猜测
with cte as
(select null link, CASE when SUBSTR(PRJA_TYPE,1,3) ='CHO' then 'CHO'
else PRJA_TYPE
END as label,
COUNT(PRJA_TYPE) Contract_Type
from "SPM"."PROJECT_AGREEMENTS"
where PRJA_ACTIVE = 1 and PRJA_TYPE IS NOT NULL
group by PRJA_TYPE
order by PRJA_TYPE
)
select link, label, sum(contract_type) contract_type from cte group by link,label
答案 2 :(得分:0)
您可以在group-clause中使用case语句:
select
null link,
CASE when SUBSTR(PRJA_TYPE,1,3) ='CHO' then 'CHO' else PRJA_TYPE END as label,
COUNT(PRJA_TYPE) Contract_Type
from "SPM"."PROJECT_AGREEMENTS"
where PRJA_ACTIVE = 1 and PRJA_TYPE IS NOT NULL
group by CASE when SUBSTR(PRJA_TYPE,1,3) ='CHO' then 'CHO' else PRJA_TYPE END
order by PRJA_TYPE
作为替代方案(例如,如果您无法修改查询),您可以使用第二个查询来汇总您的数字:
select link, label, sum(CONTRACT_TYPE)
from (
--your first query
) x
group by link, label