Solved

Adding column values recursively

Posted on 2013-06-20
3
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 49

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 41

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

691 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