Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2005, 04:43 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default updating running changes

How can I update a column in row n+1 with the running changes from row n to row n+1?

For example, I have the following table:
SubID Date Price Price Change
1 2/1/2004 10 Null
1 2/8/2004 13 3
1 2/15/2004 12 -1
2 2/1/2004 11 Null
2 2/8/2004 15 4

This is a fact table in a data warehouse of week-by-week subscriptions, and we want to report on the price changes by subscriber.

My method so far has been to put the table into a temp table, join the two tables by ID and (Date - 1 week), and update [Price Change] by the difference between the two Prices. It works, but I'm thinking there might be a better way.

tia,

Conrad




 
Old January 11th, 2006, 12:49 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

--Try this as a better way

--Setup test data

CREATE TABLE dbo.Subscription
(SUBID int
,sDate DATETIME
,Price int
,PriceChange int
)

INSERT dbo.Subscription VALUES (1, '2/1/2004', 10, NULL)
INSERT dbo.Subscription VALUES (1, '2/8/2004', 13, NULL)
INSERT dbo.Subscription VALUES (1, '2/15/2004', 12, NULL)
INSERT dbo.Subscription VALUES (2, '2/1/2004', 11, NULL)
INSERT dbo.Subscription VALUES (2, '2/8/2004', 15, NULL)

SELECT S.SUBID, S.SDate, PriorWk.SDate, S.Price, PriorWk.PRice, S.Price - PriorWk.PRice
FROM dbo.Subscription S
JOIN dbo.Subscription PriorWk
    ON S.SUBID = PriorWk.SUBID
    AND S.sDate = DATEADD(dd,7,PriorWk.sDate)

UPDATE dbo.Subscription
    SET PriceChange = Subscription.Price - PriorWk.PRice
FROM dbo.Subscription -- Not using alias to avoid Msg 8154 table name is ambiguous
JOIN dbo.Subscription as PriorWk
    ON Subscription.SUBID = PriorWk.SUBID
    AND Subscription.sDate = DATEADD(dd,7,PriorWk.sDate)

SELECT * FROM dbo.Subscription

/* Output
SUBID sDate Price PriceChange
----------- ------------------------------------------------------ ----------- -----------
1 2004-02-01 00:00:00.000 10 NULL
1 2004-02-08 00:00:00.000 13 3
1 2004-02-15 00:00:00.000 12 -1
2 2004-02-01 00:00:00.000 11 NULL
2 2004-02-08 00:00:00.000 15 4

*/

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
updating record stoneman Access 1 July 5th, 2005 09:12 AM
Updating database capilleary C# 1 June 28th, 2005 06:03 AM
updating many to many whyulil Classic ASP Databases 4 March 11th, 2004 12:58 PM
Updating a database Gary_M Classic ASP Databases 4 November 7th, 2003 11:03 AM
Updating dmurray Access 1 June 25th, 2003 05:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.