繁体   English   中英

在MySQL中查找同一列的两个连续行之间的差异

[英]Find difference between two consecutive rows of same column in mysql

我遇到一种情况,需要计算同一列的两个连续行之间的差。 这是我的表格,名为“订单”结构。

orderid   type     productsales
1002      Order    120         
1002      Refund   -35   
1003      Order    199           
1003      Refund   -50            
1004      Order    245
1005      Order    80

现在,我想要什么,只选择那些记录“productsales”相同的区别orderids订单式“订单”和“退款”是大于0

select orderid
from orders
group by orderid
having sum(case when `type` = 'Order' then productsales else 0 end) +
       sum(case when `type` = 'Refund' then productsales else 0 end) > 0

您需要从另一个减去值。 但是由于您将-存储在退款中,所以我使用+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM