0

我需要有关此查询的帮助

这是我查询的实际结果

price received   qty recieved    price release  qty release     
  10.30              10                0             0                
   0                  0              10.30           2  
   0                  0              10.30           1
  19.20              20               0              0
   0                  0              19.20           5                      
   0                  0              19.20           3                
   0                  0              19.20           1                

在我的查询中使用此代码

$result = mysql_query("SELECT *, 
     SUM(CASE WHEN qtyreceived > 0 THEN qtyreceived END) AS qtyrec,
     SUM(CASE WHEN  qtyrelease > 0 THEN qtyrelease END) AS qtyrel 
     FROM stockledger 
     WHERE stockdesc= '$_POST[desc]' GROUP BY pricerelease,pricereceived ");    

结果是

收到价格 qtyreceived 价格发布 qtyrelease bal 10.30 10 10 19.20 20 20 10.30 3 -3 19.20 9 -9

我想以这种方式查看结果,这样我就可以得到 2 个价格的剩余数字

 price received       qtyreceived    price release  qtyrelease    bal
         10.30            10              10.30           3        7
         19.20            20              19.20           9        11

谢谢.....;

4

1 回答 1

1

从你的数据来看,我认为这将基于价格。正确的?试试这个。

SELECT  a.PriceReceived,
        a.qtyreceived,
        b.priceRelease,
        b.qtyRelease,
        (a.bal + b.bal) as bal
FROM    stockledger a 
            INNER JOIN stockledger b
                ON a.priceReceived = b.priceRelease

或另一种选择是

SELECT  a.PriceReceived,
        a.qtyreceived,
        b.priceRelease,
        b.qtyRelease,
        (a.qtyreceived - b.qtyRelease) as bal
FROM    stockledger a 
            INNER JOIN stockledger b
                ON a.priceReceived = b.priceRelease

SQLFiddle 演示

于 2012-08-07T05:56:34.210 回答