Simplemente agregue la segunda consulta en la lista de columnas de la primera consulta, ya que solo devolverá una fila. Prueba esto.
SELECT Sum(CASE
WHEN MONTH = '2013-8-1' THEN CurrentStock
ELSE 0
END) AS CurrentStock,
Isnull(Sum(CASE WHEN MONTH = '2014-2-1' THEN CPU ELSE 0 END), 0) -
Isnull(Sum(CASE WHEN MONTH = '2013-8-1' THEN CPU ELSE 0 END), 0) AS CPU,
Sum(CASE WHEN Month = '2013-8-1' THEN NonCPUWIP ELSE 0 END) AS NonCPU,
Isnull(Sum(CASE WHEN MONTH = '2014-2-1' THEN ScrapDiscarded ELSE 0 END), 0) -
Isnull(Sum(CASE WHEN MONTH = '2013-8-1' THEN ScrapDiscarded ELSE 0 END), 0)AS Scrap,
Sum(CASE WHEN Month = '2013-8-1' THEN WaitingForApproval ELSE 0 END) AS WFA,
(SELECT Count(STBSerialNUMBER)
FROM OutStock
WHERE DispatchDate BETWEEN '2013-8-1' AND '2014-2-1'
AND Vendor = 'abc' ) as Despatched
FROM Instock I
WHERE Vendor = 'abc'
Actualización:
Si desea el recuento de resultados de cada modelo, convierta la subquery
a correlated subquery
. debería ser algo como
(SELECT Count(STBSerialNUMBER)
FROM OutStock O
WHERE o.model = i.model -- here is the change
AND O.DispatchDate BETWEEN '2013-8-1' AND '2014-2-1'
AND o.Vendor = 'abc')
Aquí para cada model
en Instock
tabular el respectivo count
en el outstock
será calculado.
O también puede usar Left Join
. Primero calcula el conteo de cada model
en Outstock
tabla join
el resultado de vuelta al Instock
tabla usando el modelo
....
From Instock I Left Join
(SELECT Count(STBSerialNUMBER) OutStock_count,Model
FROM OutStock O
WHERE O.DispatchDate BETWEEN '2013-8-1' AND '2014-2-1'
AND o.Vendor = 'abc'
Group by Model) O on o.model = i.model