我在Sybase中使用这个SQL来获取价格的累积值,但是当我希望它显示实际值时,它会给出空白值。
这是我的代码:
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
结果如下:
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
PRUP列为我提供了正确的值,但在之后给出了空白值。
任何想法
答案 0 :(得分:0)
我编辑了我的查询。另请参阅链接http://sqlfiddle.com/#!2/c48ec/27
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
答案 1 :(得分:0)
您需要使用ISNULL
功能
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
您也可以使用coalesce
功能。