Click here to monitor SSC
SQLServerCentral is supported by Redgate
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
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: Tuesday, October 18, 2016 9:11 AM
Points: 1, Visits: 96
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


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

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.

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: Wednesday, February 24, 2016 6:28 AM
Points: 3,977, Visits: 6,431
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.


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'

(LinkID INT, Price MONEY, CreatedDate DATETIME)

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'

SET Price = (
FROM @Table1 b
WHERE a.LinkID = b.LinkID AND Date < CreatedDate
FROM @Table2 a

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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1396444
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse