SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format
How to fill gaps in data?

Execute the following Microsoft SQL Server T-SQL script in SSMS Query Editor to generate a monthly sales summary by categories. Since gap filling used, $0.0 will show for a month when there was no sale.

USE AdventureWorks;

GO

 

CREATE PROCEDURE procSalesDatawithGapFills ( 

  @StartDate smalldatetime,

  @EndDate smalldatetime

)

AS

BEGIN

DECLARE @RunningDate smalldatetime

DECLARE @CategoriesMonths TABLE   (     

  ProductCategoryID   tinyint,  

  OrderDate    smalldatetime   

  )             

 

SET   @RunningDate = @EndDate

 

-- Insert a single date from each month in the range

WHILE (@StartDate <= @RunningDate)    

BEGIN             

  INSERT INTO @CategoriesMonths        

  SELECT PC.ProductCategoryID,   @RunningDate   

  FROM Production.ProductCategory   PC

   

  SET @RunningDate = DATEADD(mm, -1, @RunningDate)

END;

 

-- Apply a CTE to merge the place holder and available data

WITH cteSales

AS

(

 

-- Place holder sales data ($0.00) just in case no real data

SELECT     

DS.ProductCategoryID,

PC.Name as ProductCategory,

OrderDate AS Date,

0.0 AS Sales

FROM      @CategoriesMonths DS

INNER JOIN Production.ProductCategory PC

    ON DS.ProductCategoryID=PC.ProductCategoryID

 

UNION ALL

 

-- Merge in the available data          

SELECT    

PC.ProductCategoryID,

PC.Name AS ProductCategory,

SOH.OrderDate AS [Date],

SUM(SOD.UnitPrice * SOD.OrderQty) AS Sales

FROM         Production.ProductSubCategory PSC

INNER JOIN   Production.ProductCategory PC

    ON PSC.ProductCategoryID = PC.ProductCategoryID

INNER JOIN   Production.Product P

    ON PSC.ProductSubCategoryID = P.ProductSubCategoryID

INNER JOIN   Sales.SalesOrderDetail SOD

    ON P.ProductID = SOD.ProductID

INNER JOIN   Sales.SalesOrderHeader SOH

    ON SOH.SalesOrderID = SOD.SalesOrderID

 

WHERE     (SOH.OrderDate BETWEEN @StartDate AND @EndDate)

GROUP BY SOH.OrderDate, PC.Name, PSC.Name, PC.ProductCategoryID

)

 

-- Generate summaries for each month, each category

SELECT [Year]=Year([Date]),

[Month]=Month([Date]),

ProductCategory,

TotalSales = Sum(Sales)

FROM cteSales

GROUP BY Year([Date]),Month([Date]), ProductCategory

ORDER BY Year([Date]),Month([Date]), ProductCategory

END

GO

 

-- Test the stored procedure

exec procSalesDatawithGapFills '2001-01-01','2002-04-30'

GO

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.