Sql 多层次子查询中的空值istead

Sql 多层次子查询中的空值istead,sql,sybase,cumulative-sum,Sql,Sybase,Cumulative Sum,我在Sybase中使用了这个SQL来获取一个quetity的累积值乘以价格,但是当我希望它显示实际值时,它会给出空白值 这是我的密码: SELECT Pmu.IdVal, Pmu.IdInt, Pmu.IdNumEcrPpal, Pmu.IdSensOpe, Pmu.DtEcr, Pmu.QteEcr, Pmu.PrixAcquis, (SELECT SUM(p1.QteEcr) FROM casimir.dbo.Pmu p1

我在Sybase中使用了这个SQL来获取一个quetity的累积值乘以价格,但是当我希望它显示实际值时,它会给出空白值

这是我的密码:

    SELECT Pmu.IdVal, Pmu.IdInt, Pmu.IdNumEcrPpal, Pmu.IdSensOpe, Pmu.DtEcr, Pmu.QteEcr, Pmu.PrixAcquis,
    (SELECT SUM(p1.QteEcr)
            FROM   casimir.dbo.Pmu p1
            WHERE  p1.IdNumEcrPpal < Pmu.IdNumEcrPpal and p1.IdInt = Pmu.IdInt) AS QCP,
     (SELECT SUM(p2.QteEcr * p2.PrixAcquis) 
            FROM   casimir.dbo.Pmu p2
            WHERE  p2.IdNumEcrPpal < Pmu.IdNumEcrPpal and p2.IdInt = Pmu.IdInt) AS PRUP 

    FROM casimir.dbo.Pmu Pmu

    where IdInt = 1733

order by IdNumEcrPpal
PRUP列为我提供了正确的值,但后面给出了空白值


任何想法

我编辑了我的查询。请同时参考链接

您需要使用ISNULL函数


您也可以改为使用coalesce函数。

非常感谢,如果未接受null,请尝试此操作,但仍然得到相同的结果:coalescep2.QteEcr,0*coalescep2.PrixAcquis,0 where子句中可能存在一些问题。它对普鲁普没有任何价值。尝试在select子句中使用coalesce。我只是在修改我的sql。我完全删除了where子句,但仍然是同一个问题:/你能在sqlfiddle.com中放置一个示例数据吗?我这样做,但不知道如何为你提供访问权限Thank you@Parado,但我得到了与以前相同的空值Thank you,但仍然是相同的结果:/@user2583029我不知道它为什么不起作用。你能给我们看一些数据来计算你期望结果的第一行吗?
IdVal * IdInt * IdNumEcrPpal *  QteEcr *    PrixAcquis *    QCP PRUP
650     1733    1074292               69    0.00         {null} {null}
650     1733    1165538               6     0.00            69  0.00
650     1733    1618644               7     0.00            75  0.00
650     1733    1934483               10    0.00            82  0.00
650     1733    1934484               1     0.00            92  0.00
650     1733    2140552               93    0.00            93  0.00
650     1733    2506329               200   0.00            186 0.00
650     1733    2515839               100   0.00            386 0.00
650     1733    2520087               110   0.00            486 0.00
650     1733    2572565               400   0.00            596 0.00
650     1733    2581126               1     0.00            996 0.00
650     1733    2858466               56    0.00            997 0.00
650     1733    2907483               6     0.00            1053 0.00
650     1733    3227255               7     0.00            1059 0.00
650     1733    3440560               173   0.00            1066 0.00
650     1733    3440727               67    0.00            1239 0.00
650     1733    3467592               100   0.00            1306 0.00
650     1733    3482135               100   188.00          1406 0.00
650     1733    3483475               30    185.35          1506    
650     1733    3491124               350   0.00            1536    
650     1733    3717502               70    0.00            1886    
650     1733    3717503               4     0.00            1956    
650     1733    4046744               20    65.44           1960    
650     1733    4047669               200   0.00            1980    
650     1733    4059311               150   67.12           2180    
650     1733    4101861               200   0.00            2330    
650     1733    4118371               36    0.00            2530    
650     1733    4118372               3     0.00            2566    
SELECT Pmu.IdVal, Pmu.IdInt, Pmu.IdNumEcrPpal, Pmu.QteEcr, Pmu.PrixAcquis
   ,
    (SELECT SUM(p1.QteEcr)
            FROM   Pmu p1
            WHERE  p1.IdNumEcrPpal < Pmu.IdNumEcrPpal and p1.IdInt = Pmu.IdInt) AS QCP,

     coalesce ((SELECT SUM( coalesce (p2.QteEcr,0) *  coalesce (p2.PrixAcquis,0)) 
            FROM   Pmu p2
            WHERE  p2.IdNumEcrPpal < Pmu.IdNumEcrPpal and p2.IdInt = Pmu.IdInt),0) AS PRUP

 FROM Pmu Pmu

    where IdInt = 1733

order by IdNumEcrPpal
SELECT Pmu.IdVal, Pmu.IdInt, Pmu.IdNumEcrPpal, Pmu.IdSensOpe, Pmu.DtEcr, Pmu.QteEcr, Pmu.PrixAcquis,
isnull((SELECT SUM(p1.QteEcr)
        FROM   casimir.dbo.Pmu p1
        WHERE  p1.IdNumEcrPpal < Pmu.IdNumEcrPpal and p1.IdInt = Pmu.IdInt),0) AS QCP,
 isnull((SELECT SUM(isnull(p2.QteEcr,1) * isnull(p2.PrixAcquis,1)) 
        FROM   casimir.dbo.Pmu p2
        WHERE  p2.IdNumEcrPpal < Pmu.IdNumEcrPpal and p2.IdInt = Pmu.IdInt),0) AS PRUP 
FROM casimir.dbo.Pmu Pmu
where IdInt = 1733
order by IdNumEcrPpal