Select A.nEvrakKalemID As ID,
A.cEvrakKalem As [Inventory Name],
SUM((IsNull((Select SUM(B.nMiktar) Where B.nEvrakTip = 3 or B.nEvrakTip = 7),0)) - (IsNull((Select SUM(B.nMiktar) Where B.nEvrakTip = 4 or B.nEvrakTip = 8),0))) As Inventory
From MasrafAnaliz.dbo.EvrakKalem A
Left Outer Join MasrafAnaliz.dbo.EvrakKalemHareket B
On B.nKodEvrakKalem = A.nEvrakKalemID
Group By A.nEvrakKalemID, A.cEvrakKalem, B.nMiktar
Order By A.nEvrakKalemID
在上面的问题中,我想总结一下Inventory
列,并根据列nEvrakKalemID
对其进行分组。
但它不起作用。提前感谢您的帮助。
答案 0 :(得分:1)
试试这个:
Select A.nEvrakKalemID As ID,
A.cEvrakKalem As [Inventory Name],
(SUM(CASE WHEN B.nEvrakTip = 3 or B.nEvrakTip = 7
THEN (IsNull(B.nMiktar,0))
ELSE 0
END) -
SUM(CASE WHEN B.nEvrakTip = 4 or B.nEvrakTip = 8
THEN IsNull(B.nMiktar,0)
ELSE 0
END)) As Inventory
From MasrafAnaliz.dbo.EvrakKalem A
Left Outer Join MasrafAnaliz.dbo.EvrakKalemHareket B On B.nKodEvrakKalem = A.nEvrakKalemID
Group By A.nEvrakKalemID, A.cEvrakKalem, B.nMiktar
Order By A.nEvrakKalemID
答案 1 :(得分:0)
我认为你的查询应该是
SELECT
A.nEvrakKalemID AS ID,
A.cEvrakKalem AS [Inventory Name],
SUM(ISNULL((SELECT
B.nMiktar
WHERE B.nEvrakTip = 3
OR B.nEvrakTip = 7), 0) - ISNULL((SELECT
B.nMiktar
WHERE B.nEvrakTip = 4
OR B.nEvrakTip = 8), 0)) AS Inventory
FROM MasrafAnaliz.dbo.EvrakKalem A
LEFT OUTER JOIN MasrafAnaliz.dbo.EvrakKalemHareket B
ON B.nKodEvrakKalem = A.nEvrakKalemID
GROUP BY A.nEvrakKalemID,
A.cEvrakKalem,
B.nMiktar
ORDER BY A.nEvrakKalemID
使用CASE
将是更好的选择。