got2nosth
got2nosth

Reputation: 618

Fill up date gap by month

I have table of products and their sales quantity in months.

Product Month      Qty
A       2018-01-01 5
A       2018-02-01 3
A       2018-05-01 5
B       2018-08-01 10
B       2018-10-01 12
...

I'd like to first fill in the data gap between each product's min and max dates like below:

Product Month      Qty
A       2018-01-01 5
A       2018-02-01 3
A       2018-03-01 0
A       2018-04-01 0
A       2018-05-01 5
B       2018-08-01 10
B       2018-09-01 0
B       2018-10-01 12
...

Then I would need to perform an accumulation of each product's sales quantity by month.

Product Month      total_Qty
A       2018-01-01 5
A       2018-02-01 8
A       2018-03-01 8
A       2018-04-01 8
A       2018-05-01 13
B       2018-08-01 10
B       2018-09-01 10
B       2018-10-01 22
...

I fumbled over the "cross join" clause, however it seems to generate some unexpected results for me. Could someone help to give a hint how I can achieve this in SQL?

Thanks a lot in advance.

Upvotes: 0

Views: 938

Answers (5)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

I think a recursive CTE is a simple way to do this. The code is just:

with cte as (
      select product, min(mon) as mon, max(mon) as end_mon
      from t
      group by product
      union all
      select product, dateadd(month, 1, mon), end_mon
      from cte
      where mon < end_mon
     )
select cte.product, cte.mon, coalesce(qty, 0) as qty
from cte left join
     t
     on t.product = cte.product and t.mon = cte.mon;

Here is a db<>fiddle.

Upvotes: 2

Bob Dubke
Bob Dubke

Reputation: 66

First of all, i would divide month and year to get easier with statistics.

I will give you an example query, not based on your table but still helpful.


--here i create the table that will be used as calendar
Create Table MA_MonthYears (
Month int  not null ,
year int  not null 
PRIMARY KEY ( month, year) )

--/////////////////


-- here i'm creating a procedure to fill the ma_monthyears table
declare @month as int 
declare @year as int
set @month = 1
set @year = 2015

while ( @year != 2099  )
begin

insert into MA_MonthYears(Month, year)
select @month, @year

if @month < 12 
set @month=@month+1
else
set @month=1

if @month = 1 
set @year = @year + 1 

end

--/////////////////


--here you are the possible result you are looking for
select SUM(Ma_saledocdetail.taxableamount) as Sold, MA_MonthYears.month , MA_MonthYears.year , item
from MA_MonthYears left outer join MA_SaleDocDetail on year(MA_SaleDocDetail.DocumentDate) = MA_MonthYears.year 
and Month(ma_saledocdetail.documentdate) = MA_MonthYears.Month
group by MA_SaleDocDetail.Item, MA_MonthYears.year , MA_MonthYears.month
order by  MA_MonthYears.year , MA_MonthYears.month

Upvotes: 0

Sreenu131
Sreenu131

Reputation: 2516

Try this below

IF OBJECT_ID('tempdb..#Temp')  IS NOT NULL
DROP TABLE #Temp
;WITH CTE(Product,[Month],Qty)
AS
(
SELECT 'A','2018-01-01', 5  UNION ALL
SELECT 'A','2018-02-01', 3  UNION ALL
SELECT 'A','2018-05-01', 5  UNION ALL
SELECT 'B','2018-08-01', 10 UNION ALL
SELECT 'D','2018-10-01', 12
)
SELECT ct.Product,[MonthDays],ct.Qty 
INTO #Temp
FROM
(
SELECT c.Product,[Month],
       ISNULL(Qty,0) AS Qty 
FROM CTE c
)ct
RIGHT JOIN
(
 SELECT -- This code is to get month data
    CONVERT(VARCHAR(10),'2018-'+ RIGHT('00'+CAST(MONTH(DATEADD(MM, s.number, CONVERT(DATETIME, 0)))AS VARCHAR),2) +'-01',120) AS [MonthDays]         
 FROM master.dbo.spt_values s 
 WHERE [type] = 'P' AND s.number BETWEEN 0 AND 11
)DT
ON dt.[MonthDays] = ct.[Month]


SELECT   
        MAX(Product)OVER(ORDER BY [MonthDays])AS Product,
        [MonthDays],        
        ISNULL(Qty,0) Qty,
        SUM(ISNULL(Qty,0))OVER(ORDER BY [MonthDays]) As SumQty
FROM #Temp

Result

Product MonthDays   Qty SumQty
------------------------------
A       2018-01-01  5   5
A       2018-02-01  3   8
A       2018-03-01  0   8
A       2018-04-01  0   8
A       2018-05-01  5   13
A       2018-06-01  0   13
A       2018-07-01  0   13
B       2018-08-01  10  23
B       2018-09-01  0   23
D       2018-10-01  12  35
D       2018-11-01  0   35
D       2018-12-01  0   35

Upvotes: 0

Daniel Brughera
Daniel Brughera

Reputation: 1651

You can create the months with a recursive CTE

DECLARE @MyTable TABLE   
(  
    ProductID  CHAR(1),  
    Date      DATE,
    Amount      INT
) 

INSERT INTO @MyTable 
VALUES 
('A','2018-01-01', 5),
('A','2018-02-01', 3),
('A','2018-05-01', 5),
('B','2018-08-01', 10),
('B','2018-10-01', 12)

DECLARE @StartDate DATE 
DECLARE @EndDate DATE

SELECT @StartDate = MIN(Date), @EndDate = MAX(Date) FROM @MyTable

;WITH dates AS (
    SELECT @StartDate AS Date
    UNION ALL
    SELECT DATEADD(Month, 1, Date)
    FROM dates 
    WHERE Date < @EndDate   
)
SELECT A.ProductID, d.Date, COALESCE(Amount,0) AS Amount, COALESCE(SUM(Amount) OVER(PARTITION BY A.ProductID ORDER BY  A.ProductID, d.Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),0) AS Total
FROM 
(
    SELECT ProductID, MIN(date) as DateStart, MAX(date) as DateEnd 
    FROM @MyTable
    GROUP BY ProductID -- As I read in your comments that you need different min and max dates per product
) A 
JOIN dates d ON d.Date >= A.DateStart AND d.Date <= A.DateEnd
LEFT JOIN @MyTable T ON A.ProductID = T.ProductID AND T.Date =  d.Date
ORDER BY A.ProductID, d.Date

Upvotes: 0

Sanpas
Sanpas

Reputation: 1180

Hi i think this example can help you and perform what you excepted :

  CREATE TABLE #MyTable  
(Product   varchar(10),  
   ProductMonth      DATETIME,
   Qty      int
  );  
GO  

CREATE TABLE #MyTableTempDate  
(
   FullMonth      DATETIME
  );  
GO  


INSERT INTO #MyTable 
SELECT 'A', '2019-01-01',  214
UNION
SELECT 'A', '2019-02-01',  4
UNION 
SELECT 'A', '2019-03-01',  50
UNION
SELECT 'B', '2019-01-01',  214
UNION
SELECT 'B', '2019-02-01',  10
UNION 
SELECT 'C', '2019-04-01', 150


INSERT INTO #MyTableTempDate
SELECT '2019-01-01'
UNION
SELECT '2019-02-01'
UNION
SELECT '2019-03-01'
UNION
SELECT '2019-04-01'
UNION
SELECT '2019-05-01'
UNION
SELECT '2019-06-01'
UNION
SELECT '2019-07-01';

------------- FOR NEWER SQL SERVER VERSION  > 2005

WITH MyCTE AS 
(
    SELECT T.Product, T.ProductMonth AS 'MMonth', T.Qty
    FROM #MyTable T
    UNION
    SELECT T.Product, TD.FullMonth AS 'MMonth', 0 AS 'Qty'
    FROM #MyTable T, #MyTableTempDate TD
    WHERE NOT EXISTS (SELECT 1 FROM #MyTable TT WHERE TT.Product = T.Product AND TD.FullMonth = TT.ProductMonth)
)
-- SELECT * FROM MyCTE;
SELECT Product, MMonth, Qty, SUM( Qty) OVER(PARTITION BY Product ORDER BY  Product 
     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as 'TotalQty'
FROM MyCTE
ORDER BY Product, MMonth ASC;


DROP TABLE  #MyTable


DROP TABLE  #MyTableTempDate

I have other way to perform this in lower SQL Server Version (like 2005 and lower) It's a SELECT on SELECT if it's your case let me know and i provide some other example.

Upvotes: 0

Related Questions