Here is the thing, I have table like this
First one
ID1 ID2 ID3
Second
ID1 ID2 ID3 month, value
What I need is to get values for three months for each combination of these 3 IDs, and I went this way:
SELECT a.ID1, a.ID2, a.ID3, b.value, c.value, d.value
FROM Table1 a
LEFT JOIN Table2 b
ON a.ID1 = b.ID1
AND a.ID2 = b.ID2
AND a.ID3 = b.ID3
WHERE month=2
LEFT JOIN Table2 c
ON a.ID1 = c.ID1
AND a.ID2 = c.ID2
AND a.ID3 = c.ID3
WHERE month=3
LEFT JOIN Table2 d
ON a.ID1 = d.ID1
AND a.ID2 = d.ID2
AND a.ID3 = d.ID3
WHERE month=4
And I got values multiplied.
Any idea why this is case, or any suggestions for other approaches?
Thanks.
Assuming you want the next 3 months (next month and the two following months) based on today’s date, you can do this (you can also set @base to any specific date to the get the three following months):