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

x
?
Solved

Add column data from one row to another

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

610 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