我想从下表中检索car_id min(svc_date),状态= A.
预期为103和100,其状态为“A”
因为它的状态为A,最小为svc_date
car_id svc_date status
100 03/01/2013 A
100 04/02/2013 B
100 05/03/2013 C
101 06/01/2013 A
101 05/01/2013 B
102 06/06/2013 A
102 05/05/2013 B
103 05/25/2013 A
我正在使用postgres并且正在尝试
select car_id,svc_date,status from car_tbl group by
car_id having min(svc_date) and status='A'
svc_date是时区和时区的时间戳 得到错误
argument of AND must be type boolean, not type timestamp with time zone
是整个sql错误还是任何类型转换会有帮助吗?
答案 0 :(得分:2)
真的,你很难理解:
with cte as (
select
car_id ,
status,
rank() OVER (
PARTITION BY car_id
ORDER BY svc_date )
from car_tbl
)
select *
from cte
where rank = 1
and status = 'A'
| CAR_ID | STATUS | RANK |
--------------------------
| 100 | A | 1 |
| 103 | A | 1 |
答案 1 :(得分:0)
很难看出你真正在问什么;但这样做你想要的吗?
SELECT car_id, status, MIN(svc_date) FROM car_tbl WHERE status = 'A' GROUP BY car_id, status