1
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.

我该如何修复它?

4

1 回答 1

2

您不能在定义别名的同一级别上使用别名。您需要将其包装在派生表中:

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;
于 2016-03-28T16:15:35.333 回答