简体   繁体   English

SQL数量计算

[英]SQL quantity calculation

Well, I have a mind-scratcher that I cannot solve. 好吧,我有一个无法解决的问题。 Total newbie :) I need to calculate stock item quantity and detect negative values if they appear in calculation: 新手总数:)我需要计算库存项目的数量,并在计算中出现负值时进行检测:

inquantity | outquantity
100        |      0
10         |      0
0          |     50
0          |     100
20         |      0
0          |     80
15         |      0
100        |      0

And I need to calculate Quty: 我需要计算数量:

inquantity | outquantity | Quty
100        |      0      | 100
10         |      0      | 110
0          |     50      | 60
0          |     100     | -40
20         |      0      | -20
0          |     80      | -100
15         |      0      | -85
100        |      0      | 15

How can i do that ? 我怎样才能做到这一点 ?

Regarding Abhik's post: 关于Abhik的帖子:

select 
id ,
inquantity,
outquantity,
@qty:= (@qty+inquantity)-outquantity as qty 
from quantity,(select @qty:= 0 )r 
order by id;

is there a possibility to reset the variable @qty on productid change? 有可能在productid更改时重置变量@qty吗?

+----+-----------+------------+-------------+------+
| id | productid | inquantity | outquantity | qty  |
+----+-----------+------------+-------------+------+
|  1 |         1 |        100 |           0 | 100  |
|  2 |         1 |         10 |           0 | 110  |
|  3 |         1 |          0 |          50 |  60  |
|  4 |         1 |          0 |         100 | -40  |
|  5 |         2 |         20 |           0 |  20  |
|  6 |         2 |          0 |          80 | -60  |
|  7 |         2 |         15 |           0 | -45  |
|  8 |         3 |        100 |           0 | 100  |
+----+-----------+------------+-------------+------+

Consider the following as you mentioned you have id 考虑到以下提到的id

mysql> select * from quantity ;
+------+------------+-------------+
| id   | inquantity | outquantity |
+------+------------+-------------+
|    1 |        100 |           0 |
|    2 |         10 |           0 |
|    3 |          0 |          50 |
|    4 |          0 |         100 |
|    5 |         20 |           0 |
|    6 |          0 |          80 |
|    7 |         15 |           0 |
|    8 |        100 |           0 |
+------+------------+-------------+

We can get the desired result as 我们可以得到期望的结果

select 
id ,
inquantity,
outquantity,
@qty:= (@qty+inquantity)-outquantity as qty 
from quantity,(select @qty:= 0 )r 
order by id;

The output would be 输出将是

+------+------------+-------------+------+
| id   | inquantity | outquantity | qty  |
+------+------------+-------------+------+
|    1 |        100 |           0 |  100 |
|    2 |         10 |           0 |  110 |
|    3 |          0 |          50 |   60 |
|    4 |          0 |         100 |  -40 |
|    5 |         20 |           0 |  -20 |
|    6 |          0 |          80 | -100 |
|    7 |         15 |           0 |  -85 |
|    8 |        100 |           0 |   15 |
+------+------------+-------------+------+

The idea is the get the cumulative sum. 这个想法是获得累计和。 Assuming you have a column that provides ordering information, you can do this with subqueries or variables. 假设您有一列提供订购信息,则可以使用子查询或变量来完成此操作。 The latter should be more efficient: 后者应该更有效:

select t.*, (cumei - cumeo) as diff
from (select t.*, (@i := @i + inquantity) as cumei,
              (@o := @o + outquantity) as cumeo
      from table t
           (select @i := 0, @o := 0) vars
      order by id
     ) t
where (cumei - cumeo) < 0;

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

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