Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2011-03-14 : 23:21:54
|
I have a price table with the following layout:ArtNo StartDate EndDate Price----- ---------- ---------- ------123 2010-12-14 208.75123 2011-01-04 211.68123 2011-02-08 212.80123 2011-01-04 211.98234 2010-10-15 208.75234 2011-01-14 211.68234 2011-03-12 212.80543 2011-01-08 21.18543 2011-02-01 22.36 I need to update each row for every ArtNo with an End Date that is the day prior to the next StartDate with the exception of the latest Start Date which needs to be a NULL value.So after the Update the table should look like this:ArtNo StartDate EndDate Price----- ---------- ---------- ------123 2010-12-14 2011-01-03 208.75123 2011-01-04 2011-02-07 211.68123 2011-02-08 2011-03-01 212.80123 2011-03-02 211.98234 2010-10-15 2011-01-13 168.75234 2011-01-14 2011-03-11 171.68234 2011-03-12 170.80543 2011-01-08 2011-01-31 21.18543 2011-02-01 22.36 Any direction that you can give me would be helpful.Thanks. hf |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-15 : 01:16:27
|
| One way:Create table #Test(ArtNo int,StartDate datetime,EndDate datetime,Price numeric(9,2))Insert into #TestSelect 123 ,'2010-12-14',null, 208.75 Union allSelect 123 ,'2011-01-04',null, 211.68 Union allSelect 123 ,'2011-02-08',null, 212.80 Union allSelect 123 ,'2011-03-04',null, 211.98 Union allSelect 234 ,'2010-10-15',null, 208.75 Union allSelect 234 ,'2011-01-14',null, 211.68 Union allSelect 234 ,'2011-03-12',null, 212.80 Union allSelect 543 ,'2011-01-08',null, 21.18 Union allSelect 543 ,'2011-02-01',null, 22.36Update TSet EndDate = Dateadd(day , -1, ST.StartDate)From(Select Row_number() over (Partition by ArtNo Order By StartDate) Srno , * from #Test ) as Tinner join (Select Row_number() over (Partition by ArtNo Order By StartDate) Srno, * from #Test ) as STOn T.Srno + 1 = ST.Srno and T.ArtNo = ST.ArtnoSelect * from #Test order by ArtNO,StartDate |
 |
|
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2011-03-15 : 14:48:04
|
Thank you very much - that works perfectly.I have a similar scenario for a table that contains cost:ArtNo SuppNo StartDate EndDate Price----- ------ ---------- ---------- ------123 abc 2010-12-14 208.75123 abc 2011-01-04 211.68123 bcd 2011-02-08 212.80123 bcd 2011-01-04 211.98234 gfe 2010-10-15 208.75234 gfe 2011-01-14 211.68234 qaz 2011-03-12 212.80543 wsc 2011-01-08 21.18543 edc 2011-02-01 22.36 Here I also need to update the End Dates with the exception of the row with the latest StartDate but for the combination of ArtNo and SuppNo. It is possible that I get the same ArtNo from multiple suppliers and they will have their respective Start and End Dates.So the expected result would be:ArtNo SuppNo StartDate EndDate Cost----- ------ ---------- ---------- ------123 abc 2010-12-14 2011-01-04 88.75123 abc 2011-01-04 91.68123 bcd 2011-02-08 2011-03-03 92.80123 bcd 2011-03-04 91.98234 gfe 2010-10-15 2011-01-13 78.75234 gfe 2011-01-14 81.68234 qaz 2011-03-12 82.80543 wsc 2011-01-08 11.18543 edc 2011-02-01 13.36 |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-15 : 16:06:17
|
quote: Originally posted by HenryFulmer Thank you very much - that works perfectly.
You are welcome  quote: Here I also need to update the End Dates with the exception of the row with the latest StartDate but for the combination of ArtNo and SuppNo. It is possible that I get the same ArtNo from multiple suppliers and they will have their respective Start and End Dates.So the expected result would be:
Please give some try. Hint: (Partition by ArtNo,SuppNo order By StartDate)If you don't get expected result then post what you tried and either me or somebody in the forum will help you out.I can give you answer but that way you will not learn.Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2011-03-15 : 16:19:58
|
@pk_bohra: thanks for your assistance!Here is what I tried and it appears to work. Is this the correct way to do it?UPDATE t1SET EndDate = Dateadd(day , -1, t2.StartDate)FROM(SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER BY StartDate) Srno , * FROM #Teste ) AS t1INNER JOIN (SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER By StartDate) Srno, * FROM #Teste ) AS t2ON t1.Srno + 1 = t2.Srno and (t1.ArtNo = t2.Artno AND t1.SuppNo = t2.SuppNo) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-03-15 : 17:16:29
|
quote: Originally posted by HenryFulmer @pk_bohra: thanks for your assistance!Here is what I tried and it appears to work. Is this the correct way to do it?UPDATE t1SET EndDate = Dateadd(day , -1, t2.StartDate)FROM(SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER BY StartDate) Srno , * FROM #Teste ) AS t1INNER JOIN (SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER By StartDate) Srno, * FROM #Teste ) AS t2ON t1.Srno + 1 = t2.Srno and (t1.ArtNo = t2.Artno AND t1.SuppNo = t2.SuppNo)
Really? That's updateable?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2011-03-15 : 18:16:24
|
Originally posted by X002548quote: Really? That's updateable?
Why? Am I doing something incorrectly?Create table #Teste(ArtNo int,SuppNo char(10),StartDate datetime,EndDate datetime,Cost numeric(9,2))Insert into #TesterSelect 123,'abc' ,'2010-12-14',null, 208.75 Union allSelect 123,'abc' ,'2011-01-04',null, 211.68 Union allSelect 123,'bcd' ,'2011-02-08',null, 212.80 Union allSelect 123,'bcd' ,'2011-03-04',null, 211.98 Union allSelect 234,'abc' ,'2010-10-15',null, 208.75 Union allSelect 234,'abc' ,'2011-01-14',null, 211.68 Union allSelect 234,'qaz' ,'2011-03-12',null, 212.80 Union allSelect 543,'efb' ,'2011-01-08',null, 21.18 Union allSelect 543,'ert' ,'2011-02-01',null, 22.36UPDATE t1SET EndDate = Dateadd(day , -1, t2.StartDate)FROM(SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER BY StartDate) Srno , * FROM #Teste ) AS t1INNER JOIN (SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER By StartDate) Srno, * FROM #Teste ) AS t2ON t1.Srno + 1 = t2.Srno and (t1.ArtNo = t2.Artno AND t1.SuppNo = t2.SuppNo)SELECT * FROM #Teste My result set looks like this:ArtNo SuppNo StartDate EndDate Price123 abc 2010-12-14 2011-01-03 208.75123 abc 2011-01-04 NULL 211.68123 bcd 2011-02-08 2011-03-03 212.80123 bcd 2011-03-04 NULL 211.98234 abc 2010-10-15 2011-01-13 208.75234 abc 2011-01-14 NULL 211.68234 qaz 2011-03-12 NULL 212.80543 efb 2011-01-08 NULL 21.18543 ert 2011-02-01 NULL 22.36 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-03-16 : 04:44:42
|
quote: Originally posted by X002548
quote: Originally posted by HenryFulmer @pk_bohra: thanks for your assistance!Here is what I tried and it appears to work. Is this the correct way to do it?UPDATE t1SET EndDate = Dateadd(day , -1, t2.StartDate)FROM(SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER BY StartDate) Srno , * FROM #Teste ) AS t1INNER JOIN (SELECT Row_number() over (PARTITION BY ArtNo,SuppNo ORDER By StartDate) Srno, * FROM #Teste ) AS t2ON t1.Srno + 1 = t2.Srno and (t1.ArtNo = t2.Artno AND t1.SuppNo = t2.SuppNo)
Really? That's updateable?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
Are you still working in version 2000? MadhivananFailing to plan is Planning to fail |
 |
|
|
HenryFulmer
Posting Yak Master
110 Posts |
Posted - 2011-03-16 : 07:21:01
|
| I'm working in 2008 R2. |
 |
|
|
|
|
|
|
|