Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding column values recursively

Posted on 2013-06-20
3
Medium Priority
?
304 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

618 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