Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/02/2016, 09:40
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 4 meses
Puntos: 774
Respuesta: Ventas menos devoluciones

Cita:
Iniciado por ejbsoft Ver Mensaje
Hola

Qué tal algo tan simple como esto?

SELECT T0.[ItemCode], T0.[Dscription], sum(T0.[Quantity])
FROM INV1 T0 INNER JOIN OINV T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE T1.[DocDate] BETWEEN '01-01-2016' AND '31-01-2016'
GROUP BY T0.[ItemCode], T0.[Dscription]
ORDER BY T0.[ItemCode] ASC

unión all

SELECT T2.[ItemCode], T2.[Dscription], sum(T2.[Quantity])
FROM RIN1 T2 INNER JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry]
WHERE T2.[DocDate] BETWEEN '01-01-2016' AND '31-01-2016'
and t2.[itemcode] not in (select itemcode from inv1 WHERE DocDate BETWEEN '01-01-2016' AND '31-01-2016' )
GROUP BY T2.[ItemCode], T2.[Dscription]
ORDER BY T2.[ItemCode] ASC
Y la resta??? esta preguntando por como hacer la resta de las ventas menos las compras

Código SQL:
Ver original
  1. SELECT ventas.itemcode,ventas.dscription,  ventas-devoluciones AS resta FROM(
  2. SELECT T0.[ItemCode], T0.[Dscription], SUM(T0.[Quantity]) AS ventas
  3. FROM INV1 T0 INNER JOIN OINV T1 ON T0.[DocEntry] = T1.[DocEntry]
  4. WHERE T1.[DocDate] BETWEEN '01-01-2016' AND '31-01-2016'
  5. GROUP BY T0.[ItemCode], T0.[Dscription] ) AS ventas
  6. LEFT JOIN
  7. (
  8. SELECT T2.[ItemCode], T2.[Dscription], SUM(T2.[Quantity]) AS devoluciones
  9. FROM RIN1 T2 INNER JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry]
  10. WHERE T2.[DocDate] BETWEEN '01-01-2016' AND '31-01-2016'
  11. GROUP BY T2.[ItemCode], T2.[Dscription]
  12. ) AS devoluciones ON (ventas.itemcode=devoluciones.itemcode)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me