我有一個PostgreSQL的查詢是這樣的:如何獲得查詢的前10個結果?
with r as (
select
1 as reason_type_id,
rarreason as reason_id,
count(*) over() count_all
from
workorderlines
where
rarreason != 0
and finalinsdate >= '2012-12-01'
)
select
r.reason_id,
rt.desc,
count(r.reason_id) as num,
round((count(r.reason_id)::float/(select count(*) as total from r) * 100.0)::numeric, 2) as pct
from r
left outer join
rtreasons as rt
on
r.reason_id = rt.rtreason
and r.reason_type_id = rt.rtreasontype
group by
r.reason_id,
rt.desc
order by r.reason_id asc
這將返回結果的表4列:理由ID,與該原因ID相關聯的描述中,具有這個原因ID的條目的數量,並且該數字表示的總數的百分比。
此表是這樣的:
我想什麼做的是隻顯示基於關有原因ID條目總數的前10個結果。然而,不管剩下的是什麼,我想編入另一行,並加上一個名爲「其他」的描述。我將如何做到這一點?
這個工作,但我不得不改變獲得職級的行: '等級() (通過計數(r.reason_id)desc)作爲cause_rank' – 2013-02-15 19:11:02