SQL join

Hi,

I am trying to do the following:

In table 1 my data is monthly:

KEY1      VALUE1      DATE1
1      A      4/1/2005
1      B      5/1/2005
1      C      6/1/2005
1      D      7/1/2005
1      E      8/1/2005
1      F      9/1/2006


In table 2 my data is quarterly
KEY2      VALUE2      DATE2
1      a      6/1/2005
1      b      9/1/2005
1      c      12/1/2005
1      d      3/1/2006

I am trying to merge the two on KEY1=KEY2 so that I get the following structure

KEY      VALUE1      VALUE2      DATE1      DATE2
1      A      a      6/1/2005      4/1/2005
1      B      a      6/1/2005      5/1/2005
1      C      a      6/1/2005      6/1/2005
1      D      b      9/1/2005      7/1/2005
1      E      b      9/1/2005      8/1/2005
1      F      b      9/1/2005      9/1/2006


I want to merge on the Key . Each date in table 2 should get  merged with 3 dates in table 2: lag 0 month date,lag 1 months date,lag2 months date. The output should look like table3.

How can I join the table 1 and table 2 to get table 3?

Thanks
camper12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
SELECT T1.KEY1, T1.VALUE1, T2.VALUE2, T1.DATE1, T2.DATE2
FROM Table1 AS T1
INNER JOIN Table2 AS T2
   ON T1.KEY1 = T2.KEY2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperCommented:
if you're always doing the end of the quarter (eg: 3, 6, 9, 12)
the following will work:


SELECT T1.KEY1, T1.VALUE1, T2.VALUE2, T1.DATE1, T2.DATE2
FROM Table1 AS T1
INNER JOIN Table2 AS T2
   ON datepart(year, t1.Date) = datepart(year, t2.date) AND
          datepart(month,t1.date) between datepart(month,t2.date) - 2 and datepart(month,t2.date)

Open in new window



logic:
the year must be the same
and the month must be 2 months before to the current month.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
camper12, do you still need help with this question?
0
PortletPaulfreelancerCommented:
SELECT
      m.KEY1
    , m.VALUE1
    , q.VALUE2
    , m.DATE1
    , q.DATE2
FROM Table1 AS m
      INNER JOIN Table2 AS q ON m.KEY1 = q.KEY2
                  AND m.DATE1 BETWEEN DATEADD(MONTH, -2, q.DATE2) AND q.DATE2
;

Open in new window

or maybe:
SELECT
      m.KEY1
    , m.VALUE1
    , q.VALUE2
    , m.DATE1
    , q.DATE2
FROM table1 AS m
      CROSS APPLY (
            SELECT TOP (1)
                  table2.VALUE2
                , table2.DATE2
            FROM table2
            WHERE m.KEY1 = table2.KEY2
                  AND m.DATE1 BETWEEN DATEADD(MONTH, -2, table2.DATE2) AND table2.DATE2
            ORDER BY
                  DATE2
      ) AS q
;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.