Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

पिछली पंक्ति MySQL से मात्रा घटाएं

आप तालिका में शामिल हुए बिना भी इसका उपयोग कर सकते हैं:बाहर का चयन केवल कुछ स्तंभों को छिपाने के लिए है।

SELECT Quantity, `timestamp`, sold_qty
FROM (
    SELECT i.*,
    @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    @last_qty := i.`Quantity` as last_qty
    FROM InventoryTest_history i
    CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ORDER BY `timestamp`
    ) as result;

नमूना

mysql> SELECT * from InventoryTest_history;

+----+-------------+----------+---------------------+
| id | SKU         | Quantity | timestamp           |
+----+-------------+----------+---------------------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |
+----+-------------+----------+---------------------+
3 rows in set (0,02 sec)

mysql> SELECT Quantity, `timestamp`, sold_qty
    -> FROM (
    ->     SELECT i.*,
    ->     @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    ->     @last_qty := i.`Quantity` as last_qty
    ->     FROM InventoryTest_history i
    ->     CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    ->     ORDER BY `timestamp`
    ->     ) as result;
+----------+---------------------+----------+
| Quantity | timestamp           | sold_qty |
+----------+---------------------+----------+
|      985 | 2016-12-27 10:08:58 |        0 |
|      960 | 2016-12-28 10:09:52 |       25 |
|      955 | 2016-12-29 16:01:02 |        5 |
+----------+---------------------+----------+
3 rows in set (0,00 sec)

mysql> SELECT i.*,
    -> @sold_qty := GREATEST(@last_qty - i.`Quantity`,0) as sold_qty,
    -> @last_qty := i.`Quantity` as last_qty
    -> FROM InventoryTest_history i
    -> CROSS JOIN ( SELECT @last_qty := '', @sold_qty := 0) as init
    -> ORDER BY `timestamp`;
+----+-------------+----------+---------------------+----------+----------+
| id | SKU         | Quantity | timestamp           | sold_qty | last_qty |
+----+-------------+----------+---------------------+----------+----------+
|  1 | (L) U-Joint |      985 | 2016-12-27 10:08:58 |        0 |      985 |
|  2 | (L) U-Joint |      960 | 2016-12-28 10:09:52 |       25 |      960 |
|  3 | (L) U-Joint |      955 | 2016-12-29 16:01:02 |        5 |      955 |
+----+-------------+----------+---------------------+----------+----------+
3 rows in set (0,00 sec)

mysql>


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. विभिन्न डीबी में विदेशी कुंजी के साथ django मॉडल का उपयोग कैसे करें?

  2. एक व्यक्ति को कई खाते बनाने से रोकने का सबसे अच्छा तरीका

  3. @row:[email protected]+1 . का php समतुल्य क्या है?

  4. मैसकल इनर OR कंडीशन के साथ जुड़ें?

  5. इनर जॉइन कंडीशन WHERE क्लॉज या ON क्लॉज में?