SP facturacion Diaria mas acumulado

Buenas tardes
Señores recuro a ustedes nuevamente para solicitar de su experiencia tanto en SAP como SQL. La consulta es la siguiente: he creado una SP para que me muestre las facturación diaria hasta aquí todo bien el inconveniente es cuando trato de obtener el acumulado hasta el día anterior. ya que se me repite las filas ya he realizado diferente consulta sin llegar al resultado esperado.
Los acumulado que deseo obtener al día anterior que se tira el reporte es acumulado de facturación sin impuesto acumulado de ventas, acumulado de ganancia bruta además de la facturación .

A continuación le muestro el SP que realice

USE [xxxxxxxxx]
GO
/****** Object:  StoredProcedure [dbo].[VENTASDIARIAS2]    Script Date: 29/10/2020 4:13:13 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[VENTASDIARIAS2] 
@fechainicio datetime,
@fechafinal datetime

as
Begin 
 WITH prueba_cte (TipoDocumento,TABLA,Fecha,Vendedor,Factura,Cliente,
TotalSinITBM,VentasTotal,Utilidad,Gananciaporcentaje,etatus,Ref,Sector)  
AS  
( 
SELECT (case when T0.DocSubType= '--' then 'Factura' 
else 'Nota de Debito' end) as TipoDocumento,  
'OINV' AS 'Tabla', 
 T0.DocDate as Fecha,  T1.SlpName  as Vendedor,
T0.DocNum as Factura, T2.CardName as Cliente
,( T0.DocTotal-T0.VatSum-T0.[TotalExpns]) as TotalSinITBM,
 T0.DocTotal as VentasTotal,T0.GrosProfit as Utilidad,
((T0.GrosProfit/(NULLIF(T0.DocTotal,0)-T0.VatSum-T0.[TotalExpns]))*100) 
AS Gananciaporcentaje,  
 T0.DocStatus As etatus,T0.NumAtCard as Ref,T3.GroupName as Sector
 FROM OINV T0 
 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
 INNER JOIN OCRD T2 ON T0.CardCode = T2.CardCode 
 INNER JOIN OCRG T3 ON T2.GroupCode = T3.GroupCode 
WHERE  T0.DocDate between  @fechainicio  and @fechainicio 
and T0.CANCELED= 'N' 
union 
SELECT 'Nota de Credito' As TipoDocumento,
'ORIN' AS 'TABLA',
T0.DocDate as Fecha, T1.SlpName as Vendedor ,T0.DocNum as Factura , 
T2.CardName as Cliente,
(( T0.DocTotal-T0.VatSum-T0.[TotalExpns])*-1) as 'TotalSinITBM',
(T0.DocTotal*-1)as 'Ventas Total',-T0.GrosProfit as 'Utilidad',
(-T0.GrosProfit / (NULLIF(T0.DocTotal,0)-T0.VatSum-T0.[TotalExpns]*-1)*100) 
 AS 'Gananciaporcentaje',
 T0.DocStatus As 'estatus',T0.NumAtCard as 'Ref',T3.GroupName as 'Sector' 
 FROM ORIN T0  
 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode 
 INNER JOIN OCRD T2 ON T0.CardCode = T2.CardCode 
 INNER JOIN OCRG T3 ON T2.GroupCode = T3.GroupCode 
 WHERE T0.DocDate between @fechainicio  and @fechainicio  and T0.CANCELED= 'N'
 ) 
Select TipoDocumento,TABLA,Fecha,Vendedor,Factura,Cliente,
TotalSinITBM,VentasTotal,Utilidad,Gananciaporcentaje,etatus,Ref,Sector
    FROM prueba_cte 
    WHERE  Fecha between @fechainicio  and @fechainicio ;
 (SELECT 
  sum( T0.DocTotal-T0.VatSum-T0.[TotalExpns]) as 'AcumuladoSinITBM',
  sum(T0.DocTotal) as 'Acumulado Ventas Total',
  sum(T0.GrosProfit) as 'acumudadoUtilidad' 
  FROM OINV T0 
 WHERE  day month(@fechainicio ) < day(T0.DocDate) 
and month(@fechainicio ) = Month(T0.DocDate)
and year(@fechainicio )= year(T0.DocDate)  and T0.CANCELED= 'N'

union all

SELECT
sum((( T0.DocTotal-T0.VatSum-T0.[TotalExpns])*-1)) as 'AcumuladoSinITBM',
sum(T0.DocTotal*-1)as 'AcumuladoVentasTotal',
sum(T0.GrosProfit*-1) as 'AculuadoUtilidad'
 FROM ORIN T0  
WHERE  day month(@fechainicio ) < day(T0.DocDate) 
and month(@fechainicio ) = Month(T0.DocDate) 
and year(@fechainicio )= year(T0.DocDate)  and T0.CANCELED= 'N'
 )
END
 

Sin mas por el momento y que me puedan orientar
Saludos
@Willy_Caldero

Hola @ppierce,

El subtotal te sale en 2 lineas por que estas dividiendo la consula en Facturas y notas de credito, podrias colocar todo como subconsulta, y hacer las sumas.

Select SUM(TR.AcumuladoSinITBM), SUM('Acumulado Ventas Total'),...
FROM
(SELECT 
  sum( T0.DocTotal-T0.VatSum-T0.[TotalExpns]) as 'AcumuladoSinITBM',
  sum(T0.DocTotal) as 'Acumulado Ventas Total',
  sum(T0.GrosProfit) as 'acumudadoUtilidad' 
  FROM OINV T0 
 ......
Union all
SELECT
sum((( T0.DocTotal-T0.VatSum-T0.[TotalExpns])*-1)) as 'AcumuladoSinITBM',
sum(T0.DocTotal*-1)as 'AcumuladoVentasTotal',
sum(T0.GrosProfit*-1) as 'AculuadoUtilidad'
 FROM ORIN T0  
....
) as TR

Buenas tarde Ing. @Willy_Caldero
quiero darle las gracias por la ayuda y sus explicaciones que me han ayuda mucho. nuevamente le doy la gracias y que Dios lo bendiga.

esta fue las modificaciones que realice como usted me lo sugirió y todo salió perfecto.

USE [XXXXXXXXXX]
GO
/****** Object:  StoredProcedure [dbo].[VENTASDIARIAS2]    Script Date: 09/11/2020 5:56:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[VENTASDIARIAS2] 
@fechainicio datetime,
@fechafinal datetime

as
Begin 
 WITH prueba_cte (TipoDocumento,TABLA,Fecha,Vendedor,Factura,Cliente,
TotalSinITBM,VentasTotal,Utilidad,Gananciaporcentaje,etatus,Ref,Sector)  
AS  
( 
SELECT (case when T0.DocSubType= '--' then 'Factura' else 'Nota de Debito' end)
 as TipoDocumento,
       
'OINV' AS 'Tabla', 
 T0.DocDate as Fecha,  T1.SlpName  as Vendedor,T0.DocNum as Factura,
 T2.CardName as Cliente,( T0.DocTotal-T0.VatSum-T0.[TotalExpns]) as TotalSinITBM,
 T0.DocTotal as VentasTotal,T0.GrosProfit as Utilidad,
((T0.GrosProfit/(NULLIF(T0.DocTotal,0)-T0.VatSum-T0.[TotalExpns]))*100) 
AS Gananciaporcentaje,  
 T0.DocStatus As etatus,T0.NumAtCard as Ref,T3.GroupName as Sector
 FROM OINV T0 
 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
 INNER JOIN OCRD T2 ON T0.CardCode = T2.CardCode 
 INNER JOIN OCRG T3 ON T2.GroupCode = T3.GroupCode 
WHERE  T0.DocDate between  @fechainicio  and @fechainicio  and T0.CANCELED= 'N' 
union 
SELECT 'Nota de Credito' As TipoDocumento,
'ORIN' AS 'TABLA',
T0.DocDate as Fecha, T1.SlpName as Vendedor ,T0.DocNum as Factura 
, T2.CardName as Cliente
,(( T0.DocTotal-T0.VatSum-T0.[TotalExpns])*-1) as 'TotalSinITBM',
(T0.DocTotal*-1)as 'Ventas Total',-T0.GrosProfit as 'Utilidad',(-T0.GrosProfit / (NULLIF(T0.DocTotal,0)-T0.VatSum-T0.[TotalExpns]*-1)*100) AS 'Gananciaporcentaje',
 T0.DocStatus As 'estatus',T0.NumAtCard as 'Ref',T3.GroupName as 'Sector' 
 FROM ORIN T0  
 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode 
 INNER JOIN OCRD T2 ON T0.CardCode = T2.CardCode 
 INNER JOIN OCRG T3 ON T2.GroupCode = T3.GroupCode 
 WHERE T0.DocDate between @fechainicio  and @fechainicio  and T0.CANCELED= 'N'
 ) Select TipoDocumento,TABLA,Fecha,Vendedor,Factura,Cliente,TotalSinITBM,VentasTotal,Utilidad,Gananciaporcentaje,etatus,Ref,Sector
    FROM prueba_cte 
    WHERE  Fecha between @fechainicio  and @fechainicio ;
	select  SUM(TR.AcumuladoSinITBM), SUM(TR.AcumuladoVentasTotal),
SUM(TR.acumudadoUtilidad)
 FROM
 (SELECT 

  sum( T0.DocTotal-T0.VatSum-T0.[TotalExpns]) as 'AcumuladoSinITBM',
 SUm(T0.DocTotal) as 'AcumuladoVentasTotal',sum(T0.GrosProfit) as 'acumudadoUtilidad' 
  FROM OINV T0 
 --WHERE  month(@fechainicio) = Month(T0.DocDate) and year(@fechainicio)= year(T0.DocDate)  and T0.CANCELED= 'N'-- 
WHERE  month(@fechainicio ) = Month(T0.DocDate) and year(@fechainicio )= year(T0.DocDate)  and T0.CANCELED= 'N'
union all
SELECT
sum((( T0.DocTotal-T0.VatSum-T0.[TotalExpns])*-1)) as 'AcumuladoSinITBM',
sum(T0.DocTotal*-1)as 'AcumuladoVentasTotal',sum(T0.GrosProfit*-1) as 'AculuadoUtilidad'
  FROM ORIN T0  

  WHERE  month(@fechainicio ) = Month(T0.DocDate) and year(@fechainicio )= year(T0.DocDate)  and T0.CANCELED= 'N'
 ) AS TR
  
 END
 --WHERE month(@fechainicio) = Month(T0.DocDate) and year(@fechainicio)= year(T0.DocDate) and T0.CANCELED= 'N'-- 

Deseo hacerle una pregunta que no comprendo muy bien porque los campo de la SubConsulta cuando o sea los campos acumulativos cuando trato de llevarlos a Crystal Report no se muestran.

Sin mas por el momento

Saludos Cordiales

1 me gusta

Este tema se cerró automáticamente 7 días después de la última publicación. No se permiten nuevas respuestas.