Solved

Adding column values recursively

Posted on 2013-06-20
3
295 Views
Last Modified: 2013-06-20
I have a table Calendar which has only BusinessDates and excludes Sat and Sun and Holidays.
I have another table Products which has the Code, BusDate, ID, ItemSold

Need to create a join such that if the Products BusDate does not exist in the Calendar table then
add the ItemsSold to the next   BusDate row for a given ProductCode
So the items sold on a Sat, will be added to a  Sun, which will then be added to a Monday,
assuming is not a holiday.
If Monday was a holiday then it would be added to the Tuesdays bucket, and the addition would stop

Calendar
BusDate datetime

Products

ProductCode
BusDate datetime
ItemsSold int

Example
ProductCode, BusDate,    ItemSold
ABCD         06/14/2013   5
ABCD         06/15/2013   3      
ABCD         06/16/2013   2  
ABCD         06/17/2013   2  
ABCD         06/18/2013   1

The final result should be
ABCD         06/14/2013   5
ABCD         06/17/2013   7  
ABCD         06/18/2013   1
0
Comment
Question by:countrymeister
3 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39264748
results:
PRODUCTCODE	BUSDATE	ITEMSOLD
ABCD	June, 14 2013 		5
ABCD	June, 17 2013 		7
ABCD	June, 18 2013 		1

-- from this code

select
    p.ProductCode
  , ca.cdate        as BusDate
  , sum(p.ItemSold) as ItemSold
from products as p
cross apply (
              select top 1 busdate
              from calendar as c 
              where c.busdate >= p.busdate
              order by c.busdate ASC
            ) as ca (cdate)
group by
    p.ProductCode
  , ca.cdate
;

Open in new window

http://sqlfiddle.com/#!3/41cc1/7
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39264758
try this query.
;WITH CTE 
     AS (SELECT p.ProductCode, 
                p.BusDate, 
                MIN(c.BusDate) adj_busdate 
           FROM Products p 
                LEFT JOIN Calendar c 
                       ON p.BusDate <= c.BusDate 
          GROUP BY p.ProductCode, 
                   p.BusDate) 
SELECT c.ProductCode, 
       c.Adj_BusDate, 
       SUM(p.ItemSold) 
  FROM CTE c 
       JOIN Products p 
         ON c.ProductCode = p.ProductCode 
            AND c.BusDate = p.BusDate 
 GROUP BY c.ProductCode, 
          c.Adj_BusDate 

Open in new window

see the sample here: http://sqlfiddle.com/#!3/9652d/12
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 39264774
Awesome, thank you
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now