簡體   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