Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Filling missing dates

$
0
0

Hi.

I have the following scripts and I am showing the results in images.

CREATE TABLE [dbo].[DetalleLibroBancos]( [IdDetalleLibroBanco] [int] IDENTITY(1,1) NOT NULL, [IdOpBancaria] [int] NULL, [MovDebitoCOP] [numeric](30, 8) NULL, [MovCreditoCOP] [numeric](30, 8) NULL, CONSTRAINT [PK_DetalleLibroBancos] PRIMARY KEY CLUSTERED ( [IdDetalleLibroBanco] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[OperacionesBancarias]( [IDOPBANCARIA] [int] IDENTITY(1,1) NOT NULL, [FECHAVALORBANCO] [datetime] NULL, CONSTRAINT [PK_OperacionesBancarias] PRIMARY KEY CLUSTERED ( [IDOPBANCARIA] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[SaldoDiarioLibroBancos]( [IdSaldoDiarioLBanco] [int] IDENTITY(1,1) NOT NULL, [Fecha] [datetime] NULL, [SaldoInicialDia] [numeric](30, 8) NULL, [SaldoFinalDia] [numeric](30, 8) NULL, CONSTRAINT [PK_SALDODIARIOLIBROBANCOS] PRIMARY KEY CLUSTERED ( [IdSaldoDiarioLBanco] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO

SET IDENTITY_INSERT [dbo].[OperacionesBancarias] ON
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (1, CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (2, CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (3, CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (4, CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (5, CAST(0x0000A13D00000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (6, CAST(0x0000A13D00000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (7, CAST(0x0000A14100000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (8, CAST(0x0000A14100000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (9, CAST(0x0000A14100000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (10, CAST(0x0000A14100000000 AS DateTime))
INSERT [dbo].[OperacionesBancarias] ([IDOPBANCARIA], [FECHAVALORBANCO]) VALUES (11, CAST(0x0000A14200000000 AS DateTime))
SET IDENTITY_INSERT [dbo].[OperacionesBancarias] OFF
/****** Object:  Table [dbo].[DetalleLibroBancos]    Script Date: 09/16/2013 14:43:22 ******/
SET IDENTITY_INSERT [dbo].[DetalleLibroBancos] ON
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (2, 1, NULL, CAST(1.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (3, 2, CAST(7.00000000 AS Numeric(30, 8)), NULL)
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (4, 3, CAST(5.00000000 AS Numeric(30, 8)), NULL)
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (5, 4, NULL, CAST(2.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (6, 5, NULL, CAST(8.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (7, 6, NULL, CAST(1.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (8, 7, CAST(7.00000000 AS Numeric(30, 8)), NULL)
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (9, 8, CAST(5.00000000 AS Numeric(30, 8)), NULL)
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (10, 9, NULL, CAST(2.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (11, 10, NULL, CAST(8.00000000 AS Numeric(30, 8)))
INSERT [dbo].[DetalleLibroBancos] ([IdDetalleLibroBanco], [IdOpBancaria], [MovDebitoCOP], [MovCreditoCOP]) VALUES (12, 11, NULL, CAST(3.00000000 AS Numeric(30, 8)))
SET IDENTITY_INSERT [dbo].[DetalleLibroBancos] OFF

When I execute this:

;
  WITH  base
          AS ( SELECT   FECHAVALORBANCO ,
                        SUM(MovDebitoCOP) - SUM(MovCreditoCOP) AS saldofinal ,
                        SUM(MovDebitoCOP) sum_MovDebitoCOP ,
                        SUM(MovCreditoCOP) AS sum_MovCreditoCOP
               FROM     DetalleLibroBancos
                        INNER JOIN OperacionesBancarias ON DetalleLibroBancos.IdOpBancaria = OperacionesBancarias.IDOPBANCARIA
               GROUP BY FECHAVALORBANCO
             ),
        new
          AS ( SELECT   a.FECHAVALORBANCO ,
                        ISNULL(b.saldofinal, 0.00) start ,
                        ISNULL(a.saldofinal, 0.00) final ,
                        ISNULL(a.sum_MovDebitoCOP, 0.00) sum_MovDebitoCOP ,
                        ISNULL(a.sum_MovCreditoCOP, 0.00) sum_MovCreditoCOP
               FROM     base a
                        LEFT OUTER JOIN base b ON a.FECHAVALORBANCO = b.FECHAVALORBANCO+ 1
             )
    INSERT  INTO dbo.SaldoDiarioLibroBancos
            ( Fecha ,
              SaldoInicialDia ,
              SaldoFinalDia
	        )
            SELECT  FECHAVALORBANCO ,
                    start Saldoinicialdia ,
                    start + sum_MovDebitoCOP - sum_MovCreditoCOP saldofinaldia
            FROM    new
            ORDER BY 1

I get this table

The table that I need is this table:

When movdebitocop is not null it sums if movcreditocop is not null it substract.

Saldoinicialdia is the value of saldofinaldia of the previous day and saldofinaldia is saldoinicialdia of the current day sum or substract depending of the movements of the current day.

For example for 2013-01-02 saldoinicialdia is 9 because saldofinaldia of the previous day is 9. Saldofinaldia for 2013-01-05 is 0 because there were two movcreditocop  movements in 2013-01-05  -8-1=-9         then 9-9=0

I don't know how to fill the missing dates.

How can I do to get the required table?

Thanks.





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>