我有一个查询,它将测试的Pass / Fails每周的音量显示为两个单独的列。我想添加第三列,显示失败的百分比 - (1 /(失败+通过))*失败。
SELECT to_char(DATE,'YYYY'), to_char(DATE,'WW'),
SUM(case WHEN Result = 'Y' then 1 else 0 end) Failed,
SUM(case WHEN Result = 'N' then 1 else 0 end) Passed
FROM Result_table
GROUP BY to_char(DATE,'YYYY'), to_char(DATE,'WW')
ORDER BY to_char(DATE,'YYYY'), to_char(DATE,'WW');
我相信我必须使用上面的子查询(因此已经定义了Failed和Passed),但是,我不断收到错误 - 所以我不确定这是否是正确的方法。
非常感谢任何帮助。
干杯
答案 0 :(得分:1)
要将其用作子查询,您需要将order by
移动到外部查询,这意味着您需要年份和周列,因此要别名:
SELECT year_num, week_num, failed, passed,
100 * failed / (failed + passed) as failed_percent
FROM (
SELECT to_char(DATE,'YYYY') as year_num,
to_char(DATE,'WW') as week_num,
SUM(case WHEN Result = 'Y' then 1 else 0 end) as failed,
SUM(case WHEN Result = 'N' then 1 else 0 end) as passed
FROM Result_table
GROUP BY to_char(DATE,'YYYY'), to_char(DATE,'WW')
)
ORDER BY year_num, week_num;