我有一个像下面这样的表(vh_allocation)
vh_name | va_date | trip_no
-----------+------------+---------
Vehicle-01 | 2019-01-15 | 1
Vehicle-01 | 2019-01-15 | 1
Vehicle-01 | 2019-01-16 | 1
Vehicle-02 | 2019-01-17 | 0
我的预期结果
vh_name | va_date | trip_no
-----------+------------+---------
Vehicle-01 | 2019-01-15 | 2
Vehicle-01 | 2019-01-16 | 1
Vehicle-02 | 2019-01-17 | 0
我正在使用(sum(trip_no) over (partition by va_date )) trip_sum
查询,但得到的结果如下
vh_name | va_date | trip_no
-----------+------------+---------
Vehicle-01 | 2019-01-15 | 2
Vehicle-01 | 2019-01-15 | 2
Vehicle-01 | 2019-01-16 | 1
Vehicle-02 | 2019-01-17 | 0
答案 0 :(得分:2)
仅使用sum()
聚合和group by
select vh_name ,va_date ,sum( trip_no) as trip_no
from tablename
group by vh_name , va_date