我需要帮助。 我有这样的餐桌订单
id,order_date,price,customer_id
1 01.01.2001 100 1
2 01.02.2001 0 1
3 20.02.2001 0 1
4 04.04.2001 200 1
我需要这样的选择结果
id,order_date,price,customer_id,somefield
1 01.01.2001 100 1 100
2 01.02.2001 0 1 100
3 20.02.2001 0 1 100
3 04.04.2001 200 1 200
尝试这样的sql
select a.id,order_date,
coalesce(a.price,0) price,
customer_id
sum(coalesce(a.price,0)) OVER (order by a.order_date) somefield,
from tb_orders a
where a.customer_id=4583 and a.orderstate = 1
order by a.order_date
但是结果给出了这个
id,order_date,price,customer_id,somefield
1 01.01.2001 100 1 100
2 01.02.2001 0 1 100
3 20.02.2001 0 1 100
3 04.04.2001 200 1 300
答案 0 :(得分:3)
您可以创建子组:
SELECT *, MAX(price) OVER(PARTITION BY grp) AS somefield
FROM (
select a.id,order_date,
coalesce(a.price,0) price,
customer_id,
sum(CASE WHEN price = 0 THEN 0 ELSE 1 END) OVER (order by a.order_date) grp
from tb_orders a
where a.customer_id=4583
and a.orderstate = 1
) sub
order by order_date;