我想獲得從子查詢返回的結果的總數。這是我的查詢:如何獲取Postgresql中子查詢返回的結果總數?
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
我該怎麼辦呢?我正在使用Postgresql 9.1。謝謝!
這兩個建議工作,謝謝! – 2013-02-13 18:57:48