I need to discount or download quantities, I have a query that yields the following result:
This result I have to download the balance based on the amount, the column to calculate is descontar
; that is, for an article I have an amount of 65 and three balances 50, 30 and 10 therefore the column to discount should be at 50, 15 and 0, example:
The result of query I put it in a table and I copy it below:
CREATE TABLE [dbo].[prueba_descarga](
[articulo] [VARCHAR](10) NULL,
[cantidad] [DECIMAL](16, 6) NULL,
[lote] [VARCHAR](10) NULL,
[saldo] [DECIMAL](16, 6) NULL,
[fecha] [DATETIME] NULL,
[descontar] [DECIMAL](16, 6) NULL)
INSERT [dbo].[prueba_descarga] ([articulo], [cantidad], [lote], [saldo], [fecha], [descontar]) VALUES (N'A0001', CAST(65.000000 AS Decimal(16, 6)), N'452', CAST(50.0000000 AS Decimal(22, 7)), CAST(N'2018-06-07T00:00:00.000' AS DateTime), CAST(0.000000 AS Decimal(16, 6)))
INSERT [dbo].[prueba_descarga] ([articulo], [cantidad], [lote], [saldo], [fecha], [descontar]) VALUES (N'A0001', CAST(65.000000 AS Decimal(16, 6)), N'455', CAST(30.0000000 AS Decimal(22, 7)), CAST(N'2018-06-05T00:00:00.000' AS DateTime), CAST(0.000000 AS Decimal(16, 6)))
INSERT [dbo].[prueba_descarga] ([articulo], [cantidad], [lote], [saldo], [fecha], [descontar]) VALUES (N'A0001', CAST(65.000000 AS Decimal(16, 6)), N'456', CAST(10.0000000 AS Decimal(22, 7)), CAST(N'2018-06-11T00:00:00.000' AS DateTime), CAST(0.000000 AS Decimal(16, 6)))
INSERT [dbo].[prueba_descarga] ([articulo], [cantidad], [lote], [saldo], [fecha], [descontar]) VALUES (N'A0002', CAST(20.000000 AS Decimal(16, 6)), N'457', CAST(15.0000000 AS Decimal(22, 7)), CAST(N'2018-06-11T00:00:00.000' AS DateTime), CAST(0.000000 AS Decimal(16, 6)))
INSERT [dbo].[prueba_descarga] ([articulo], [cantidad], [lote], [saldo], [fecha], [descontar]) VALUES (N'A0002', CAST(20.000000 AS Decimal(16, 6)), N'458', CAST(25.0000000 AS Decimal(22, 7)), CAST(N'2018-06-11T00:00:00.000' AS DateTime), CAST(0.000000 AS Decimal(16, 6)))
Thank you very much !!!