@Willy_Caldero Buenas tardes Recuro a ustedes nuevamente para que me puedan orientar en un en procedimiento almacenado que estoy realizando para luego llevarlo a Crystal Report. Con el mismo debo Obtener las ventas totales por me por cada cliente por Artículos mas las cantidades vendidos. Realice el Query con dos Pivot el mismo me trae los resultados esperado, el inconveniente es que los cliente se repetir. Doy un ejemplo si un cliente compro el articulo en mes de enero y luego compro el articulo el mes de febrero este no se muestra en una misma fila sino que se muestra en dos filas para cada venta.
A continuación le muestro el query.
/* SELECT FROM [dbo].[OINV] T8 */
DECLARE @fechainicio AS DATE
/* WHERE */
SET @fechainicio = /* T8.DocDate */'[%1]'
/* SELECT FROM [dbo].[OINV] T9 */
DECLARE @fechafin AS DATE
/* WHERE */
SET @fechafin = /* T9.DocDate */'[%2]'
/* SELECT FROM [dbo].[INV1] T10 */
DECLARE @catalogo AS VARCHAR(30)
/* WHERE */
SET @catalogo = /* T10.[ItemCode] */'[%0]'
;WITH TR as
(
select * from
(
select T1.[Dscription],T0.[CardName],month(T0.DocDate) as 'mes',
DATENAME(month,T0.DocDate) as 'mescant',
SUM(T1.[LineTotal]-(T1.[LineTotal]*T0.[DiscPrcnt]/100))as 'TotVta',
(case
WHEN T1.UseBaseUn = 'Y' then SUM(T1.[Quantity])
WHEN t1.usebaseun = 'N' then
SUM(T1.[Quantity]) *T2.[NumInSale] end)AS 'cantidad'
FROM OINV T0
INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode]
/* INNER JOIN OCRD T3 ON T2.[CardCode] = T3.[CardCode]*/
WHERE T0.CANCELED = 'N' AND T1.[ItemCode]=@catalogo
and T0.DocDate between @fechainicio and @fechafin
GROUP BY T0.[CardName],T0.DocDate,T1.[Dscription],
T1.UseBaseUn,T2.[NumInSale],T1.[Quantity]
Union all
select T1.[Dscription] ,T0.[CardName],
month(T0.DocDate) as 'mes', DATENAME(month,T0.DocDate) as 'mescant',
-SUM(T1.[LineTotal]-(T1.[LineTotal]*T0.[DiscPrcnt]/100)) as 'TotVta',
(case
WHEN T1.UseBaseUn = 'Y' then -SUM(T1.[Quantity])
WHEN t1.usebaseun = 'N' then
-SUM(T1.[Quantity]) *T2.[NumInSale] end)AS 'cantidad'
FROM ORIN T0
INNER JOIN RIN1 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode]
/*INNER JOIN OCRD T3 ON T2.[CardCode] = T3.[CardCode]*/
WHERE T0.CANCELED = 'N' AND T1.[ItemCode]=@catalogo
and T0.DocDate between @fechainicio and @fechafin
GROUP BY T0.[CardName],T0.DocDate,T1.[Dscription],
T1.UseBaseUn,T2.[NumInSale],T1.[Quantity]
) as TP
PIVOT (SUM(TotVta) FOR Mes IN ([1],[2],[3],[4],[5],[6],[7],[8],[9]
,[10],[11],[12])) AS pvrt
PIVOT (SUM(cantidad) FOR mescant IN ([January],[February],
[March],[April],[May],[June],[July],[August],
[September],[October],[November],[December])) as pvrt
)
select /*TR.[Dscription],*/ TR.[CardName],
ISNULL([1],0) as 'Enero',ISNULL([January],0) AS 'CantEnero',
ISNULL([2],0) as 'Febrero', ISNULL([February],0) AS 'CantFeb',
ISNULL([3],0) as 'Marzo', ISNULL([March],0) AS 'CantMarz',
ISNULL([4],0) as 'Abril',ISNULL([April],0) AS 'CantAbril',
ISNULL([5],0) as 'Mayo',ISNULL([May],0) AS 'Mayo',
ISNULL([6],0) as 'Junio',ISNULL([June],0) AS 'CantJunio',
ISNULL([7],0) as 'Julio', ISNULL([July],0) AS 'CantJulio',
ISNULL([8],0) as 'Agosto',ISNULL([August],0) AS 'CantAgosto',
ISNULL([9],0) as 'Septiembre', ISNULL([September],0) AS 'CantSept',
ISNULL([10],0) as 'Octubre',ISNULL([October],0) AS 'CantOctubre',
ISNULL([11],0) as 'Noviembre', ISNULL([November],0) AS 'CantNov',
ISNULL([12],0) as 'Diciembre',ISNULL([December],0) AS 'CantDic',
(ISNULL([1],0)+ISNULL([2],0)+ISNULL([3],0)+ISNULL([4],0)+
ISNULL([5],0)+ISNULL([6],0)+
ISNULL([7],0)+ISNULL([8],0)+ISNULL([9],0)+ISNULL([10],0)+
ISNULL([11],0)+ISNULL([12],0)) as 'Total'
from TR
/* INNER JOIN INV1 TI1 ON TI1.[DocEntry] = TR.[DocEntry]
INNER JOIN OITM TCO ON TI1.[ItemCode] = TCO.[ItemCode]
INNER JOIN OCRD TOC ON TCO.[CardCode] = TOC.[CardCode]*/
group by TR.[Dscription],TR.CardName,TR.[1],TR.[2],TR.[3],
TR.[4],TR.[5],TR.[6],TR.[7],TR.[8],TR.[9],
TR.[10],TR.[11],TR.[12],
TR.[January],TR.[February],TR.[March],TR.[April],TR.[May],
TR.[June],TR.[July],TR.[August],
TR.[September],TR.[October],TR.[November],TR.[December]
Sin mas y en espera que me puedan orientar
Saludos Cordiales