方法1:
SELECT
(select SUM(quantity) from Sales join Document on Sales.IdDocument = Document.IdDocument
where (IdTipoDocumento = 'vco' or IdTipoDocumento = 'vcr') and IdArticulo = '01-00321' and
IdListaEmpresa = 1000129)
-
(select SUM(quantity) from Sales join Document on Sales.IdDocument = Document.IdDocument
where IdTipoDocumento = 'dco' and IdArticulo = '01-00321' and IdListaEmpresa = 1000129)
方法2:
SELECT SUM(SQ)
FROM (
SELECT CASE WHEN (IdTipoDocumento = 'vco' or IdTipoDocumento = 'vcr') THEN SUM(Quantity)
WHEN IdTipoDocumento = 'dco' THEN -1*SUM(Quantity)
END AS SQ
FROM Sales join Document on Sales.IdDocument = Document.IdDocument
where (IdTipoDocumento IN ('vco', 'vcr','dco')) and IdArticulo = '01-00321' and
IdListaEmpresa = '1000129'
GROUP BY IdTipoDocumento) AS T1
方法3:
SELECT SUM(CASE WHEN IdTipoDocumento IN ('vco', 'vcr') THEN Quantity ELSE -1*Quantity END)
AS SQ
FROM Sales join Document on Sales.IdDocument = Document.IdDocument
where (IdTipoDocumento IN ('vco', 'vcr','dco')) and IdArticulo = '01-00321' and
IdListaEmpresa = 1000129
方法3A:
SELECT IdListaEmpresa,
SUM(CASE WHEN IdTipoDocumento IN ('vco', 'vcr')
THEN Quantity
ELSE -1*Quantity END) AS SQ
FROM Sales join Document on Sales.IdDocument = Document.IdDocument
WHERE (IdTipoDocumento IN ('vco', 'vcr','dco'))
AND IdArticulo = '01-00321'
AND IdListaEmpresa IN (1000129, 1000130, 1,2,3,4,5)
GROUP BY IdListaEmpresa
什麼是你的錯誤號碼? –
@MuhammedAli Msg 8114,Level 16,State 5,Line 1 – ConnorU
「IdListaEmpresa」的數據類型是什麼? – RBarryYoung