sql >> Databasteknik >  >> RDS >> Mysql

hur man får rätt räkning

Du letar efter en kumulativ summalösning:

Kolla här:http://rextester.com/SEZS4911

select if (@last_order <> sales_order, @acm_order := order_quantity - bill_qty, @acm_order := @acm_order - bill_qty) as Open,
       sales_order,
       so_create_on,
       order_quantity,
       bill_item,
       bill_qty,
       bill_doc,
       bill_doc_date,
       @last_order := sales_order
from
     (select @last_order := 0, @acm_order := 0) x,
     (SELECT sa.sales_order,
               sa.so_create_on,
               sa.order_quantity,
               sh.bill_item, 
               sh.bill_qty,
               sh.bill_doc, 
               sh.bill_doc_date
        FROM sales_order sa
        LEFT JOIN shipped_order sh ON sa.sales_order = sh.sales_order
        order by sa.sales_order, sh.bill_doc_date) y
;



  1. 4 sätt att lista alla vyer i en SQL Server-databas

  2. Hur skickar man kommaseparerade värden till lagrad procedur i MySql?

  3. Android SQLite problem - tabell ... har ingen kolumn namngiven

  4. mysql_connect() misslyckas med tom varning