Buenas tardes
me gustaría que me ayudaran en una consulta que esto realizando el cual se muestre el total de ventas por cliente al mes restando los impuestos y las notas de créditos. A demás me muestre el tipo de cliente ya sea PRIVADA, GOBIERNO, EXTERIOR ETC. y al final muestre el total acumulado de todos los meses
mas o menos este el formato.
cod cli, ncliente Tipo Ene, febrero, marzo abril mayo junio julio agos setp oct nov dic Total
0000 PP Gob 5 5 5 5 5 5 5 5 5 5 5 5 60
0001 MM Priv 4 4 4 4 4 4 4 4 4 4 4 4 48
Esta es la consulta que hecho pero la misma no me esta filtrando por el tipo de gobierno ni me esta restado el impuesto ni las notas de creditos. a continuacion le muestro la consulta.
declare @fecha AS DATETIME
declare @fecha1 AS DATETIME
declare @tipo AS nVarChar
set @fecha = /* T.DocDate */ '[%0]'
set @fecha = /* T.DocDate */ '[%1]'
set @tipo = /* t1.groupname */ '[%3]'
SELECT T0.[CardCode],T0.[CardName],T1.GroupName,
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 1 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Enero',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 2 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode] ),0)'Febrero',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 3 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Marzo',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 4 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Abril',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 5 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Mayo',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 6 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Junio',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 7 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Julio',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 8 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Agosto',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 9 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode]= T0.[CardCode]),0)'Septiembre',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 10 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Octubre',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 11 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode] = T0.[CardCode]),0)'Noviembre',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND MONTH(T.DocDate) = 12 AND YEAR(T.DocDate)=YEAR(@fecha) AND T.[CardCode]= T0.[CardCode]),0)'Diciembre',
ISNULL((SELECT SUM(T.DocTotal-T.Vatsum) FROM OINV T WHERE T.[DocDate] >=[%0] AND T.[DocDate] <=[%1] AND YEAR(@fecha)=2020 AND T.[CardCode] = T0.[CardCode] ),0)'Acumulado 2020'
FROM OCRD T0 INNER JOIN OINV T ON T0.[CardCode] = T.[CardCode] INNER JOIN ORIN T2 ON T0.[CardCode] = T2.[CardCode] INNER JOIN OCRG T1 ON T0.[GroupCode] = T1.[GroupCode]
GROUP BY T0.[CardCode],T0.[CardName], T2.[CardCode],T1.GroupName ORDER BY T0.[CardCode],T0.[CardName]
Les ruego me puedan ayudar ya que soy nuevo en mundo de sap y no soy muy experto en esto.
SIn mas por el momento gracias.