on 03-21-2012 10:05 PM
Jovenes expertos tengo un query que suma la cantidad de las ordenes de compra y de venta que esta con status abierto pero necesito hacer que al final de esto realice una suma del Onhand con la cantidad de la orden de compra y resta la cantidad de las ordenes de venta, pero la verdad lo hace pero no lo hacer les doy un ejemplo:
Nombre articulo | SWW | OnHand | ENE_OC | ENE_OV | Total ENE | Dato Correcto |
---|---|---|---|---|---|---|
Estufa | 0000 | 2 | 1 | 0 | 3.00 | 3 |
Gabinete | 0000 | 16.60 | 16 | 1 | 35.40 | 31.60 |
LCD 32" | 0000 | 2 | 0 | 0 | 0000 | 2.00 |
LCD 22" | 0000 | 6 | 0 | 0 | 6.00 | 6.00 |
CALENTADOR | 0000 | 3 | 0 | 0 | 2 | 3.00 |
CLOSET | 0000 | 1 | 0 | 1 | 0.000 | 0.00 |
Como podran ver me da y no me da datos correctos, pero la verdad no se el porque ya he intentado hacer de varias formas pero el resultado siempre es el mismo, les agradeceria bastante me pudieran ayudad o me indicaran si ahy otra forma de hacerlo.
El query que estoy utilizando es el siguiente:
Query |
---|
SELECT DISTINCT T0.[ItemCode], T0.[ItemName], T0.SWW, T0.[OnHand], (Select sum(Y.Quantity) FROM POR1 Y WHERE Y.[LineStatus] ='O' and Month(Y.[DocDate])= '01' and Y.itemcode = T0.itemcode) as 'ENE_OC', (Select sum(Y.Quantity) FROM RDR1 Y WHERE Y.[LineStatus] ='O' and Month(y.[DocDate])= '01' and Y.itemcode = T0.itemcode) as 'ENE_OV', Case T0.ItemCode When T0.ItemCode Then (- (Select sum(Z.Quantity) FROM RDR1 Z WHERE Month(Z.[DocDate])= '01' and Z.itemcode = T0.itemcode )+ (Select sum(Y.Quantity) FROM POR1 Y WHERE Month(Y.[DocDate])= '01' and Y.itemcode = T0.itemcode ) +(T0.Onhand)-1) end as 'TOTAL_ENE' FROM OITM T0 INNER JOIN POR1 T1 ON T0.ItemCode = T1.ItemCode INNER JOIN RDR1 T2 ON T0.ItemCode = T2.ItemCode Where T0.OnHand > '0' Group BY T0.[ItemCode], T0.[ItemName], T0.SWW, T0.[OnHand], T1.[LineStatus], T2.[LineStatus], T1.ItemCode,T2.ItemCode, T1.Quantity, T2.Quantity |
De antemano les agradesco un monton su ayuda.
Saludos y gracias por su tiempo.
Hola, intenta con la siguiente (tal vez no sea la mejor):
SELECT DISTINCT T0.[ItemCode], T0.[ItemName], T0.SWW, T0.[OnHand],
isnull((Select sum(isnull(Y.Quantity,0)) FROM POR1 Y WHERE Y.[LineStatus] ='O' and Month(Y.[DocDate])= '01' and Y.itemcode = T0.itemcode),0)
as 'ENE_OC',
isnull((Select sum(isnull(Y.Quantity,0)) FROM RDR1 Y WHERE Y.[LineStatus] ='O' and Month(y.[DocDate])= '01' and Y.itemcode = T0.itemcode),0)
as 'ENE_OV',
T0.[OnHand]-(isnull((Select sum(isnull(Y.Quantity,0)) FROM RDR1 Y WHERE Y.[LineStatus] ='O' and Month(y.[DocDate])= '01' and Y.itemcode = T0.itemcode),0))+ isnull((Select sum(isnull(Y.Quantity,0)) FROM POR1 Y WHERE Y.[LineStatus] ='O' and Month(Y.[DocDate])= '01' and Y.itemcode = T0.itemcode),0)
FROM OITM T0 INNER JOIN
POR1 T1 ON T0.ItemCode = T1.ItemCode INNER JOIN
RDR1 T2 ON T0.ItemCode = T2.ItemCode
Where T0.OnHand > '0'
Group BY T0.[ItemCode], T0.[ItemName], T0.SWW, T0.[OnHand], T1.[LineStatus], T2.[LineStatus],
T1.ItemCode,T2.ItemCode, T1.Quantity, T2.Quantity
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
100 | |
12 | |
11 | |
6 | |
6 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.