PostgreSQL - 错误:列不存在SQL状态:42703

时间:2016-05-24 15:34:24

标签: sql postgresql

我正在尝试进行同期群分析,并根据承租人的第一个租赁年度(=租房者第一次租用的年份)比较平均租金数量。基本上,我问的问题是:我们是否保留第一年租房的租房者,而不是第一年是2015年的租房者?

这是我的代码:

SELECT renter_id, 
       Min(Date_part('year', created_at)) AS first_rental_year, 
       ( Count(trip_finish) )             AS number_of_trips 
FROM   bookings 
WHERE  state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' ) 
  AND  first_rental_year = 2013 
GROUP  BY 1 
ORDER  BY 1; 

我得到的错误信息是:

ERROR:  column "first_rental_year" does not exist
LINE 6: ... 'aboard', 'ashore', 'concluded', 'disputed') AND first_rent...
                                                             ^

********** Error **********

ERROR: column "first_rental_year" does not exist
SQL state: 42703
Character: 208

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

SELECT renter_id,
       Count(trip_finish) AS number_of_trips 
FROM (
        SELECT renter_id, 
               trip_finish,
               Min(Date_part('year', created_at)) AS first_rental_year
        FROM   bookings 
        WHERE  state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' ) 
     ) T
WHERE first_rental_year = 2013  
GROUP  BY renter_id
ORDER  BY renter_id ;