Al entrar ayer a este fórum buscaba como hacer consultas para hacer un análisis de ventas, seguí las recomendación que dieron y aquí les comparto el query que hice hoy y que me dio resultados.
Por favor cualquier recomendación que quieran hacerme es bienvenida.
– Reporte que muestra las Ventas Netas por cliente en el 2017 y calcula % que representa el total vendido al cliente en relación al monto total vendido en el año
Declare @Venta2017 as numeric
SET @Venta2017= ( (Select SUM (T3.LineTotal)
FROM OINV T2 INNER JOIN INV1 T3 ON T2.DocEntry = T3.DocEntry
where T2.DocDate between '20170101' and '20171231') + (SELECT case when SUM (T5.LineTotal)*-1 < 0 THEN SUM (T5.LineTotal)*-1 ELSE 0 END
FROM ORIN T4 INNER JOIN RIN1 T5 ON T4.DocEntry = T5.DocEntry
where T4.DocDate between '20170101' and '20171231'))
Select T2. Cardcode 'Cliente', T8.CardName 'Nombre Cliente',
SUM (T3.LineTotal) 'Total Facturado 2017',
(SELECT case when SUM (T5.LineTotal)*-1 < 0 THEN SUM (T5.LineTotal)*-1 ELSE 0 END
FROM ORIN T4 INNER JOIN RIN1 T5 ON T4.DocEntry = T5.DocEntry
where T4.DocDate between '20170101' and '20171231' and T2. cardcode=T4.CardCode) 'Notas de Crédito 2017',
SUM (T3.LineTotal) +
(SELECT case when SUM (T5.LineTotal)*-1 < 0 THEN SUM (T5.LineTotal)*-1 ELSE 0 END
FROM ORIN T4 INNER JOIN RIN1 T5 ON T4.DocEntry = T5.DocEntry
where T4.DocDate between '20170101' and '20171231' and T2.cardcode=T4.CardCode) 'Venta Neta 2017',
(SUM (T3.LineTotal) + (SELECT case when SUM (T5.LineTotal)*-1 < 0 THEN SUM (T5.LineTotal)*-1 ELSE 0 END
FROM ORIN T4 INNER JOIN RIN1 T5 ON T4.DocEntry = T5.DocEntry
where T4.DocDate between '20170101' and '20171231' and T2.cardcode=T4.CardCode)) / @Venta2017*100 as'% del total vendido'
FROM OINV T2
INNER JOIN INV1 T3 ON T2.DocEntry = T3.DocEntry
INNER JOIN OCRD T8 ON T8.[CardCode]= T2.[CardCode]
where T2.DocDate between '20170101' and '20171231'
Group By T2.CardCode, T8.CardName
Order By 5 Desc