我正在尝试以假设为基础进行计算,所以我假设我可以使用函数的情况完成此操作。如:
SELECT
TRIM(EXTRACT(YEAR FROM business_date))||'Q'||TRIM((EXTRACT(MONTH FROM business_date)+2)/3) as year_qtr,
case when orr_txt = '1' then (sum(cast (rr9m as float)) / (sum(cast (total_observations as float)) - 0.5*sum(cast (lft as float)) )) end as DR_1,
case when orr_txt = '2' then (sum(cast (rr9m as float)) / (sum(cast (total_observations as float)) - 0.5*sum(cast (lft as float)) )) end as DR_2,
case when orr_txt = '3' then (sum(cast (rr9m as float)) / (sum(cast (total_observations as float)) - 0.5*sum(cast (lft as float)) )) end as DR_3,
avg(cast (dp as float)) as Pred_PD
FROM
raroc.pd_matrixRR a,
raroc.ccc_dp_sc b,
raroc.ratings_convert c
WHERE
start_orr_code in (1)
and case when a.scorecard_group = 'BB' then 'Business Banking' else a.scorecard_group end= b.rating_sys
and a.orr_txt = b.bacrr
and a.orr_txt = c.bacrr_text
and orr_txt not in ('9-', '10', '11', '12', 'NR')
and rating_sys_grp = 'Scorecard'
--and a.orr_txt = '1'
--and scorecard_group = 'Large Corporation'
--and scorecard_group = 'Large Corporation_FI'
--and scorecard_group = 'Middle Market'
--and scorecard_group = 'BB'
--and scorecard_group = 'GCSBB'
--and scorecard_group = 'Real Estate'
and scorecard_group = 'Individuals'
and scorecard_group not like 'Sove%'
and scorecard_group not like 'Lega%'
--and year_qtr < '2014Q2'
group by 1,2,3,4
ORDER BY 2,1
显然没有编译并嘲笑我的逻辑。但是,如果我不对它进行分组,我将收到错误3604。问题是,我如何得到这个来计算第1,2和3类的比率。 非常感谢!
SP
答案 0 :(得分:1)
我认为你只想要条件聚合。条件 inside 聚合函数。我认为这是你想要的表达式(当然每个值重复3次:
SELECT . . .
(sum(case when orr_txt = '1' then cast(rr9m as float) end) /
sum(case when orr_txt = '1' then cast(total_observations as float) end) -
0.5 * sum(case when orr_txt = '1' then cast(lft as float) end)
) as DR_1, . . .
然后,您可以将GROUP BY
更改为GROUP BY 1
。