Solved

Adding column values recursively

Posted on 2013-06-20
3
301 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 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 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Incorrect syntax near the keyword 'INNER' 3 32
export sql server dbs 2 21
T-SQL: Wrong Result 7 33
Any benefit to adding a Clustered index here? 4 32
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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

740 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