Hola Compañeros
Después de que me pudiera ayudar un compañero del foro pude terminar mi consulta por mes clientes y artículos.
Lista los clientes filtra por articulo en cuestión y los pone pos mes, suma por mes y hace un promedio anual.
Espero les sea de utilidad ya solo pueden jugar o mover las condiciones para modificar para adecuarlo a sus necesidades.
Saludos
DECLARE @articulo varchar(100) = ''
DECLARE @mes INT= datepart(month,getdate())
set @articulo = (Select itemcode from OITM Z2 where Z2.Itemcode ='[%0]')
SELECT
CardCode as 'Codigo de Cliente', CardName as 'Nombre del Cliente', ItemCode as 'Codigo del Articulo',
ISNULL([1], 0.00) as 'Enero',
ISNULL([2], 0.00) as 'Febrero',
ISNULL([3], 0.00) as 'Marzo',
ISNULL([4], 0.00) as 'Abril',
ISNULL([5], 0.00) as 'Mayo',
ISNULL([6], 0.00) as 'Junio',
ISNULL([7], 0.00) as 'Julio' ,
ISNULL([8], 0.00) as 'Agosto',
ISNULL([9], 0.00) as 'Septiembre',
ISNULL([10], 0.00) as 'Octubre',
ISNULL([11], 0.00) as 'Noviembre',
ISNULL([12], 0.00) as 'Diciembre',
(isnull([1],0)+ISNULL([2], 0.00)+ISNULL([3], 0.00)+ISNULL([4], 0.00)+ISNULL([5], 0.00)+ISNULL([6], 0.00)+ISNULL([7], 0.00)+ISNULL([8], 0.00)+ISNULL([9], 0.00)+ISNULL([10], 0.00)+ISNULL([11], 0.00)+ISNULL([12], 0.00)) as Total,
((isnull([1],0)+ISNULL([2], 0.00)+ISNULL([3], 0.00)+ISNULL([4], 0.00)+ISNULL([5], 0.00)+ISNULL([6], 0.00)+ISNULL([7], 0.00)+ISNULL([8], 0.00)+ISNULL([9], 0.00)+ISNULL([10], 0.00)+ISNULL([11], 0.00)+ISNULL([12], 0.00)) / @mes) as Promedio
FROM
( SELECT oinv.CardName, oinv.CardCode, inv1.ItemCode, (inv1.Quantity ) as CANTIDAD, DATEPART(MONTH,oinv.DocDate) DATA_MES
FROM OINV INNER JOIN INV1 ON OINV.DocEntry = INV1.DocEntry AND YEAR(oinv.DocDate) = YEAR(GETDATE())
INNER JOIN OCRD ON OINV.CardCode = OCRD.CardCode
WHERE
inv1.ItemCode= @articulo and OCRD.GroupCode='100' /*Aqui pueden modificar por grupo o quitarlo para que les salgan todos los clientes */
UNION ALL
SELECT
orin.CardName, orin.CardCode, rin1.ItemCode, (rin1.quantity *-1) as CANTIDAD, DATEPART(MONTH,orin.DocDate) DATA_MES
FROM
ORIN INNER JOIN RIN1 ON ORIN.DocEntry = RIN1.DocEntry AND Year(orin.Docdate) = YEAR(GETDATE())
INNER JOIN OCRD ON ORIN.CardCode = OCRD.CardCode
WHERE
RIN1.ItemCode= @articulo and OCRD.GroupCode='100' )
/*Aqui pueden modificar por grupo o quitarlo para que les salgan todos los clientes */
As Mes_table PIVOT (SUM(CANTIDAD) FOR DATA_MES IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS PIV_MES
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 |