I want to create an SP with two parameters, Date and Bank, the purpose is that each bank must show (among other data) all the clients in that bank and the amount of cash that will be available in 24.48 and 72 hours. after the Date parameter. The question is that I have a table where some general data of the accounts are stored and another table where I store the flows, from the second table I "calculate" the future flow that will be for each period (24,48,72)
and I try to show you this in a temporary table of 3 columns, then I try to join this table with the general data table with left join
. What I have has the following structure:
CREATE PROCEDURE .dbo.SALDOS_24_48_72 @pmInstitución nvarchar(25), @pmFechaDeConsulta date
as
Declare @24hrs date,@48hrs date, @72hrs date, @96hrs date
, @pmFechadeConsulta date='2017-02-14' --EJEMPLO
DECLARE @pmInstitución nvarchar(50)='Banco' --EJEMPLO
Set @24hrs=(SELECT DATEADD(DAY,1,@pmFechaDeConsulta)) --24 horas después de la fecha de consulta
Set @48hrs=(SELECT DATEADD(DAY,2,@pmFechaDeConsulta)) --48 horas después de la fecha de consulta
Set @72hrs=(SELECT DATEADD(DAY,3,@pmFechaDeConsulta)) --72 horas después de la fecha de consulta
Set @96hrs=(SELECT DATEADD(DAY,4,@pmFechaDeConsulta)) --96 horas después de la fecha de consulta
;
WITH #Liquidacion as (
(SELECT SUM(importe) as UnDia,A.Cliente from MovimientosEDGE A LEFT JOIN pruebaEDGE.dbo.Clientes B ON A.Cliente=B.Clave Left join pruebaEDGE.dbo.CuentasEDGE C on A.Cliente=C.Cliente WHERE
C.Proveedor=@pmInstitución and B.Activo=1 and Liquidacion>@pmFechaDeConsulta and liquidacion< @48hrs GROUP BY A.Cliente) --Efectivo 24 hrs
UNION
(SELECT SUM(importe) as DosDias,A.Cliente from MovimientosEDGE A LEFT JOIN pruebaEDGE.dbo.Clientes B ON A.Cliente=B.Clave Left join pruebaEDGE.dbo.CuentasEDGE C on A.Cliente=C.Cliente WHERE
C.Proveedor=@pmInstitución and B.Activo=1 and Liquidacion> @24hrs and liquidacion< @72hrs GROUP BY A.Cliente) --Efectivo 48 hrs
UNION
(SELECT SUM(importe) as TresDias,A.cliente from MovimientosEDGE A LEFT JOIN pruebaEDGE.dbo.Clientes B ON A.Cliente=B.Clave Left join pruebaEDGE.dbo.CuentasEDGE C on A.Cliente=C.Cliente WHERE
C.Proveedor=@pmInstitución and B.Activo=1 and Liquidacion> @48hrs and liquidacion< @96hrs GROUP BY A.Cliente) --Efectivo 72 hrs
)
SELECT A.Cliente,A.Alias,Cuenta,Valor,Saldo,C.UnDia,C.DosDias,C.TresDias FROM CuentasEDGE A LEFT JOIN Clientes B ON A.Cliente=B.Clave LEFT JOIN #Liquidacion C ON A.Cliente=C.Cliente WHERE
B.Activo=1 and A.Proveedor=@pmInstitución
In the Declarations I use an example of date and Bank to execute the code but it returns an ERROR:
Msg 207, Level 16, State 1, Line 28 Invalid column name 'TwoDays'. Msg 207, Level 16, State 1, Line 28 Invalid column name 'ThreeDays'.
That is, it does not recognize the values of the temporary table. One issue that I observed is that I do not have records in the flow table for 48 and 72 hours. Is the fact that these columns are empty the reason for the error? Is there a better way to generate this final table? I want, in the end, to have this form:
Customer | Alias | Account | Value | Balance | 24hrs | 48 hrs | 72 hrs
00001 | Red | 000666 | 1421 | 63.50 | 125 | 0 | 0 |