Solved

Add column data from one row to another

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 27
Sql server lock cursor 13 41
sql server query? 6 26
How to iterate through each record without using cursor 2 22
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.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 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

19 Experts available now in Live!

Get 1:1 Help Now