Postgres 9.4 的中位数 - 为什么我的代码不工作?
Median with Postgres 9.4 - Why isn't my code working?
SELECT
(users.created_at::timestamp - invitations.created_at::timestamp) as elapsed,
percentile_disc(0.50)
WITHIN GROUP (ORDER BY elapsed) as median
FROM invitations
join users on users.id = invitations.user_id
试图找出从创建邀请到用户接受邀请之间的平均等待时间。此代码生成错误:
ERROR: column "elapsed" does not exist.
如何修复?
您不能在定义别名的同一级别上使用别名。您需要将其包装在派生的 table:
中
select elapsed,
percentile_disc(0.50) WITHIN GROUP (ORDER BY elapsed) as median
from (
SELECT users.created_at::timestamp - invitations.created_at::timestamp as elapsed,
FROM invitations
join users on users.id = invitations.user_id
) t;
SELECT
(users.created_at::timestamp - invitations.created_at::timestamp) as elapsed,
percentile_disc(0.50)
WITHIN GROUP (ORDER BY elapsed) as median
FROM invitations
join users on users.id = invitations.user_id
试图找出从创建邀请到用户接受邀请之间的平均等待时间。此代码生成错误:
ERROR: column "elapsed" does not exist.
如何修复?
您不能在定义别名的同一级别上使用别名。您需要将其包装在派生的 table:
中select elapsed,
percentile_disc(0.50) WITHIN GROUP (ORDER BY elapsed) as median
from (
SELECT users.created_at::timestamp - invitations.created_at::timestamp as elapsed,
FROM invitations
join users on users.id = invitations.user_id
) t;