サブクエリから返された結果の総数を取得しようとしています。これは私の質問です:
select
count(r.reason_id) as num,
cast (count(r.reason_id) as float) / (select count(*) as total from r) * 100.0 as pct
from (
select
case
when rarreason != 0 then rarreason
else rejectreason end as reason_id
from
workorderlines
where
(rarreason != 0 or rejectreason != 0)
) as r
group by
r.reason_id
ただし、これを実行しようとすると、次のエラーが発生します。
ERROR: relation "r" does not exist
LINE 3: ...on_id) as float) / (select count(*) as total from r) * 100.0...
^
********** Error **********
ERROR: relation "r" does not exist
SQL state: 42P01
Character: 112
どうすればよいですか?Postgresql9.1を使用しています。ありがとう!