Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update a table by compare values in two rows from a second table Expand / Collapse
Author
Message
Posted Thursday, December 6, 2012 3:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 27, 2014 5:44 AM
Points: 1, Visits: 47
Couldn't think of a more specific subject for my query so apologies in advance.
Would really appreciate help with this.

I have the following two tables. I need to write an Update statement to update Table2.Price with the Table2.Price. The condition here is that Table2.CreatedDate the Table1.Date rows.
E.g the first row in Table2 the CreatedDate is greater than Table1.Date so the Table2.Price would be 1500.
E.g row 4 in Table1 the CreatedDate is Greater than row 3 in Table1.Date and less than row 2 in Table1.Date so the price would be 1000

Table1

LinkID Price Date
1166 1500 2012-11-15 00:00:00
1166 1300 2012-10-01 00:00:00
1166 1000 2012-05-15 00:00:00
2020 2000 2012-09-09 00:00:00
2020 1750 2012-07-01 00:00:00


Table2
LinkID Price CreatedDate
1166 NULL 2012-12-01 00:00:00
1166 NULL 2012-10-05 00:00:00
1166 NULL 2012-10-01 00:00:00
1166 0 2012-08-02 00:00:00
2020 NULL 2012-10-09 00:00:00
2020 NULL 2012-07-02 00:00:00


Following is how the updated Table2 should look like.

Table2
LinkID Price CreatedDate
1166 1500 2012-12-01 00:00:00
1166 1300 2012-10-05 00:00:00
1166 1300 2012-10-01 00:00:00
1166 1000 2012-08-02 00:00:00
2020 2000 2012-10-09 00:00:00
2020 1750 2012-07-02 00:00:00

Thanks everyone in advance.
Post #1393773
Posted Thursday, December 13, 2012 5:47 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:58 PM
Points: 3,422, Visits: 5,366
Probably the reason no one has volunteered to help after 6 days is that you didn't provide your sample data in readily consumable form.

I'm feeling charitable today so I will do that for you.

DECLARE @Table1 TABLE
(LinkID INT, Price MONEY, Date DATETIME)

INSERT INTO @Table1
SELECT 1166,1500,'2012-11-15 00:00:00'
UNION ALL SELECT 1166,1300,'2012-10-01 00:00:00'
UNION ALL SELECT 1166,1000,'2012-05-15 00:00:00'
UNION ALL SELECT 2020,2000,'2012-09-09 00:00:00'
UNION ALL SELECT 2020,1750,'2012-07-01 00:00:00'

DECLARE @Table2 TABLE
(LinkID INT, Price MONEY, CreatedDate DATETIME)

INSERT INTO @Table2
SELECT 1166,NULL,'2012-12-01 00:00:00'
UNION ALL SELECT 1166,NULL,'2012-10-05 00:00:00'
UNION ALL SELECT 1166,NULL,'2012-10-01 00:00:00'
UNION ALL SELECT 1166,0,'2012-08-02 00:00:00'
UNION ALL SELECT 2020,NULL,'2012-10-09 00:00:00'
UNION ALL SELECT 2020,NULL,'2012-07-02 00:00:00'

UPDATE a
SET Price = (
SELECT TOP 1 Price
FROM @Table1 b
WHERE a.LinkID = b.LinkID AND Date < CreatedDate
ORDER BY Date DESC)
FROM @Table2 a

SELECT *
FROM @Table2


You can Google "SQL correlated subquery" if you need to understand how this works.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1396444
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse