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

Filling gaps when calculate running total

$
0
0

Hello Friends,

i have a script that calculate running total,but i have gaps between dates.i had some solutions to resolve this issue but it did not perform well.because my source table has over milllion rows.please see my query,output and desired output.

Thanks in advance.

IF OBJECT_ID('tempdb..#SalesSummary') IS NOT NULL
	DROP TABLE #SalesSummary

IF OBJECT_ID('tempdb..#StockDetail') IS NOT NULL
	DROP TABLE #StockDetail

IF OBJECT_ID('dbo.SalesTransaction') IS NOT NULL
	DROP TABLE SalesTransaction

IF OBJECT_ID('dbo.DesiredOutput') IS NOT NULL
	DROP TABLE DesiredOutput

CREATE TABLE [dbo].[SalesTransaction] (
	[StoreKey] [int] NULL
	,[StockKey] [int] NULL
	,[OptionKey] [int] NULL
	,[DateKey] [int] NULL
	,[Quantity] [int] NULL
	)

CREATE TABLE [dbo].[DesiredOutput] (
	[Datekey] [int] NULL
	,[StoreKey] [int] NULL
	,[StockKey] [int] NULL
	,[OptionKey] [int] NULL
	,[SalesFlag] [int] NOT NULL
	,[Quantity] [int] NULL
	,[StockQty] [int] NULL
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	1
	,1
	,1
	,20140601
	,20
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	1
	,1
	,1
	,20140603
	,- 10
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	1
	,1
	,1
	,20140607
	,30
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	2
	,2
	,2
	,20140602
	,15
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	2
	,2
	,2
	,20140603
	,- 5
	)

INSERT [dbo].[SalesTransaction] (
	[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[DateKey]
	,[Quantity]
	)
VALUES (
	2
	,2
	,2
	,20140605
	,20
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140601
	,1
	,1
	,1
	,1
	,20
	,20
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140602
	,1
	,1
	,1
	,0
	,0
	,20
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140603
	,1
	,1
	,1
	,1
	,- 10
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140604
	,1
	,1
	,1
	,0
	,0
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140605
	,1
	,1
	,1
	,0
	,0
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140606
	,1
	,1
	,1
	,0
	,0
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140607
	,1
	,1
	,1
	,1
	,30
	,40
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140602
	,2
	,2
	,2
	,1
	,15
	,15
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140603
	,2
	,2
	,2
	,1
	,- 5
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140604
	,2
	,2
	,2
	,0
	,0
	,10
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140605
	,2
	,2
	,2
	,1
	,20
	,30
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140606
	,2
	,2
	,2
	,0
	,0
	,30
	)

INSERT [dbo].[DesiredOutput] (
	[Datekey]
	,[StoreKey]
	,[StockKey]
	,[OptionKey]
	,[SalesFlag]
	,[Quantity]
	,[StockQty]
	)
VALUES (
	20140607
	,2
	,2
	,2
	,0
	,0
	,30
	)

SELECT Datekey
	,StoreKey
	,StockKey
	,OptionKey
	,SUM(Quantity) Quantity
INTO #SalesSummary
FROM dbo.SalesTransaction
GROUP BY Datekey
	,StoreKey
	,StockKey
	,OptionKey

SELECT Datekey
	,StoreKey
	,StockKey
	,OptionKey
	,1 AS SalesFlag
	,Quantity
	,SUM(Quantity) OVER (
		PARTITION BY StoreKey
		,StockKey
		,OptionKey ORDER BY Datekey ROWS UNBOUNDED PRECEDING
		) AS StockQty
INTO #StockDetail
FROM #SalesSummary
ORDER BY Datekey

SELECT *
FROM #StockDetail

SELECT *
FROM DesiredOutput


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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