?
Solved

Add column data from one row to another

Posted on 2013-06-20
3
Medium Priority
?
317 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
[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 13

Assisted Solution

by:alpmoon
alpmoon earned 750 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 750 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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…
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 …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

762 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