我做了一个简单的查询,检查客户购买单身人士的金额:
SELECT k.voornaam || ' ' || k.familienaam as "Naam", sum( b.aantal * s.prijs) as "Omzet"
from bestellingen b
left join klanten k on k.klantid = b.klantid
left join singles s on s.singleid = b.singleid
where b.klantid = 12
group by k.voornaam,k.familienaam
返回:
但我还需要名为type customer
的第三列>
我必须发现与其他所有客户相比,这笔收入有多好
所以我需要计算每个客户的收入,看看它与它们的比较。
我该怎么做? 我也可以使用PL / SQL,如果这更适合这项工作,因为我正在构建一个oracle项目(apex)。
thx:)
答案 0 :(得分:1)
我认为Oracle Apex具有Oracle的基础功能。您可以使用百分位分析函数执行此操作:
with t as (
SELECT k.voornaam || ' ' || k.familienaam as "Naam",
sum( b.aantal * s.prijs) as "Omzet"
from bestellingen b left join
klanten k
on k.klantid = b.klantid left join
singles s
on s.singleid = b.singleid
where b.klantid = 12
group by k.voornaam,k.familienaam
)
select t.*,
(case when percentile between 0 and 0.25 then 'VeryGood'
when percentile between 0.25 and 0.5 then 'Good'
when percentile between 0.5 and 0.75 then 'Average'
else 'Bad'
end) as CustGrp
from (select t.*,
percent_rank() over (order by omzet desc) as percentile
from t
) t
我假设“omzet”意味着收入。