Solved

Add column data from one row to another

Posted on 2013-06-20
3
310 Views
Last Modified: 2013-06-20
I have a two tables Products and BusinessDates

Products
ProductCode
BusDate datetime
ID int
ItemsSold int

BusinessDates
BusDate datetime

I would like to add the ItemsSold for all the Products whose date is not in BusinessDates
to the next business date for a given Product and ID
Example

BusinessDates
06/14/2013
06/17/2013
06/18/2013


Products
XYZ
06/14/2013
123
3

XYZ
06/15/2013
123
10

XYZ
06/16/2013
123
5

XYZ
06/17/2013
123
12

Final result set
XYZ
06/14/2013
123
3

XYZ
06/17/2013
123
27
0
Comment
Question by:countrymeister
3 Comments
 
LVL 13

Assisted Solution

by:alpmoon
alpmoon earned 250 total points
ID: 39263823
I haven't tested, but I think you need something like below

update Products
  from Products P1
   set ItemsSold + (select sum(P2.ItemSold) from Products P2
                     where P2.BusDate is not in (select BusDate from BusinessDates)
                       and P2.Busdate > (select max(B.BusDate) from BusinessDates B
                                          where B.BusDate < P1.BusDate)
                       and P2.Busdate < P1.Busdate)
                            )
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 250 total points
ID: 39264096
countrymeister,
Here is a Select query - I have added a column that shows what will be NewTotalOfItemsSoldForTheDay. If this looks fine, this can easily be changed to an update query.
 
SELECT a.Product, a.ProductId, a.BusDate, a.ItemsSold
          ,a.ItemsSold + (select sum(ItemsSold) 
              from Products b 
            where b.Product = a.Product 
                and b.ProductId=a.ProductId 
                and b.Busdate < a.BusDate
                and b.Busdate > (Select Max(BusDate) from BusDates c where c.BusDate < a.BusDate)
             ) AS NewTotalOfItemsSoldForTheDay
FROM Products a
 where exists (Select BusDate from BusDates d where d.BusDate = a.BusDate)
Order by 1,2,3

Open in new window

0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 39264272
My question was incorrect. Going to post again
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

929 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

13 Experts available now in Live!

Get 1:1 Help Now