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 |
|
KidSQL
Yak Posting Veteran
88 Posts |
Posted - 2005-04-07 : 12:32:09
|
| I thought I had this figured out yesterday, but I've run into a new variant this morning and can't figure out why I'm having trouble.I have two tables with historical data. One table has annual records and the other has monthly records. I'm trying to join the two such that the annual records repeat for each month for a given id until the next annual record should appear (which means repeat the annual record 12 times until the next successive record should appear next to the corresponding month). To envision things, I've got:MonthlyTable:mo1 Item1mo2 Item2mo3 Item3AnnualTable:yr1 Item1yr2 Item2yr3 Item3I am trying to join these two tables such that the AnnualTable's records appear chronologically in the correct place between the MonthlyTable's dates and are replicated however many times they must be until the weekly date record requires the next month's record (which should mean 12 times per unique id in each table). In other words, if I have:Annual records of:October 1, 2001October 1, 2002 October 1, 2003Monthly Records of :Jul 1, 2002Aug 1, 2002Sep 1, 2002Oct 1, 2002Nov 1, 2002Dec 1, 2002I want to get:Monthly Date Annual DateJul 1, 2002.............October 1, 2001Aug 1, 2002.............October 1, 2001Sep 1, 2002.............October 1, 2001Oct 1, 2002.............October 1, 2002Nov 1, 2002.............October 1, 2002Dec 1, 2002.............October 1, 2002(etc until)Oct 1, 2003.............October 1, 2003Again, the key is to repeat the correct annual record the appropriate number of times per monthly record.I would be tremendously grateful to the kind soul who could help me through this. |
|
|
rfrancisco
Yak Posting Veteran
95 Posts |
Posted - 2005-04-07 : 12:38:00
|
| Try this one:SELECT A.Date, B.DateFROM Monthly A INNER JOIN Annual BON A.Date BETWEEN B.Date AND DATEADD(D, -1, DATEADD(YY, 1, B.Date)) |
 |
|
|
KidSQL
Yak Posting Veteran
88 Posts |
Posted - 2005-04-07 : 12:56:45
|
| This worked again! As before, I am very grateful for your help. Thank You. I hope to be able to return the favor some day. |
 |
|
|
|
|
|
|
|