Solved

Add column data from one row to another

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

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 23
SQL Dump exec output to table 3 22
sql server query 18 39
RESTORE MASTER DATABASE -- NOW 2 20
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

861 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