Ing. Willy
Usted me disculpa pero no se que estoy realizando mal y es sobre todo en la subconsulatas que me esta enviando el error. le envio las consulta para pedirle el favor me la revise y me pueda orientar donde estoy comentiendo el erro
sin mas por el momento y esperando me pueda ayudar
/* SELECT FROM [dbo].[OINV] T1 */
DECLARE @fechainicio AS DATE
/* WHERE */
SET @fechainicio = /* T1.DocDate */'[%1]'
/* SELECT FROM [dbo].[OINV] T2 */
DECLARE @fechafin AS DATE
/* WHERE */
SET @fechafin = /* T2.DocDate */'[%2]'
;WITH TR as
(
select * from
(
/*select T0.CardCode, Month(T0.DocDate) as 'Mes', ((T0.DocTotal-T0.Vatsum-T0.[TotalExpns])-(T0.DocTotal*T0.[DiscPrcnt]/100)) as 'TotVta'*/
select /*T0.CardCode,*/ Month(T0.DocDate) as 'Mes', SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns]) as 'TotVta'
FROM OINV T0
INNER JOIN OCRD T0C on T0C.CardCode = T0.CardCode
/*INNER JOIN OCRG T0G on T0G.GroupCode = T0C.GroupCode and T0G.GroupName = @gname*/
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin GROUP BY T0.DocDate
Union all
/*select T1.CardCode, Month(T1.DocDate) as 'Mes', ((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])-(T1.DocTotal*T1.[DiscPrcnt]/100)*-1) as 'TotVta'*/
select /*T1.CardCode,*/ Month(T1.DocDate) as 'Mes', SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1) as 'TotVta'
FROM ORIN T1
INNER JOIN OCRD T1C on T1C.CardCode = T1.CardCode
/*INNER JOIN OCRG T1G on T1G.GroupCode = T1C.GroupCode and T1G.GroupName = @gname*/
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin GROUP BY T1.DocDate
) as TP
PIVOT (SUM(TP.TotVta) FOR TP.Mes IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) pvrt
)
select 'TOTAL MENSUAL', /*TC.CardCode,TC.CardName,TG.GroupName,*/
ISNULL([1],0) AS 'Enero', ISNULL([2],0) as 'Febrero', ISNULL([3],0) as 'Marzo', ISNULL([4],0) as 'Abril',
ISNULL([5],0) as 'Mayo', ISNULL([6],0) as 'Junio', ISNULL([7],0) as 'Julio', ISNULL([8],0) as 'Agosto',
ISNULL([9],0) as 'Septiembre', ISNULL([10],0) as 'Octubre', ISNULL([11],0) as 'Noviembre', ISNULL([12],0) as 'Diciembre',
(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 OCRD TC on TC.CardCode = TR.CardCode ORDER BY 1*/
/*inner join OCRG TG on TC.GroupCode = TG.GroupCode*/
UNION ALL
Select(
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=1)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=1)
) as 'Enero',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=2)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=2)
) as 'Febrero',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=3)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=3)
) as 'Marzo',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=4)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=4)
) as 'Abril',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=5)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=5)
) as 'Mayo,
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=6)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=6)
) as 'Junio',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=7)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=7)
) as 'Julio',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=8)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=8)
) as 'Agosto'
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=9)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=9)
) as 'Septiembre',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=10)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=10)
) as 'Octubre',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=11)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=11)
) as 'Noviembre',
(select SUM(T0.DocTotal-T0.Vatsum-T0.[TotalExpns])
FROM OINV T0
WHERE T0.CANCELED = 'N' and T0.DocDate between @fechainicio and @fechafin
AND Month (T0.DocDate) <=12)
UNION ALL
(select SUM((T1.DocTotal-T1.Vatsum-T1.[TotalExpns])*-1)
FROM ORIN T1
WHERE T1.CANCELED = 'N' and T1.DocDate between @fechainicio and @fechafin
AND Month (T1.DocDate) <=12)
) as 'Diciembre'
)