我试图使用公共表表达式使用PostgreSQL中的percentile_cont()函数计算百分位数。目标是找到前1%的帐户关于他们的余额(这里称为金额)。我的逻辑是找到99%的百分位数,这将返回那些帐户余额超过其同行99%的人(从而找到1个百分点)
这是我的查询
--ranking subquery works fine
with ranking as(
select a.lname,sum(c.amount) as networth from customer a
inner join
account b on a.customerid=b.customerid
inner join
transaction c on b.accountid=c.accountid
group by a.lname order by sum(c.amount)
)
select lname, networth, percentile_cont(0.99) within group
order by networth over (partition by lname) from ranking ;
我一直收到以下错误。
ERROR: syntax error at or near "order"
LINE 2: ...ame, networth, percentile_cont(0.99) within group order by n..
我想也许我忘记了一个闭合支架等,但我似乎无法弄清楚在哪里。我知道它可能是order
关键字的东西,但我不知道该怎么做。你能帮我修一下这个错误吗?
这也使我绊倒了。
事实证明,postgres 9.3不支持percentile_cont
,只有9.4+。
https://www.postgresql.org/docs/9.4/static/release-9-4.html
所以你必须使用这样的东西:
with ordered_purchases as (
select
price,
row_number() over (order by price) as row_id,
(select count(1) from purchases) as ct
from purchases
)
select avg(price) as median
from ordered_purchases
where row_id between ct/2.0 and ct/2.0 + 1
关于https://www.periscopedata.com/blog/medians-in-sql的查询(部分:“Postgres中位数”)
您缺少within group (order by x)
部分中的括号。
试试这个:
with ranking
as (
select a.lname,
sum(c.amount) as networth
from customer a
inner join account b on a.customerid = b.customerid
inner join transaction c on b.accountid = c.accountid
group by a.lname
order by networth
)
select lname,
networth,
percentile_cont(0.99) within group (
order by networth
) over (partition by lname)
from ranking;
我想指出你不需要子查询:
select c.lname, sum(t.amount) as networth,
percentile_cont(0.99) within group (order by sum(t.amount)) over (partition by lname)
from customer c inner join
account a
on c.customerid = a.customerid inner join
transaction t
on a.accountid = t.accountid
group by c.lname
order by networth;
此外,当使用表别名(应该总是)时,表缩写比任意字母更容易遵循。