I couldn't understand your requirements, even if I saw your examples.
For example:
1) Code in your result include 'L'. But, it was not explained in your description of the conditions.
(Yours include a description of 'c'.)
2) I thought that period 2 satisfied condition 2). But, your example showed code 'L'.
3) I thought that period 3 satisfied condition 1). But, your example showed code 'L'.
4) Condition 3) for code 'c' was inconsistent with the description of code 'c' in your original post.
Here is my understandings about relationships of periods:
Code:
Month |3(March) 4(April) 5(May) 6(June) 7(Jury)
------+------------+------------------------------+-------------------------------+------------------------------+----------
Date |2 33 0 1 2 3 0 1 2 33 0 1 2 3 0 1
|012345678901 123456789012345678901234567890 1234567890123456789012345678901 123456789012345678901234567890 1234567890
------+------------+------------------------------+-------------------------------+------------------------------+----------
1 | 3/27--------first----------4/22 code=a
1a | 3/26-----------a------------4/22
1b | 4/09-----------b------------5/06
------+------------+------------------------------+-------------------------------+------------------------------+----------
2 | 5/10------first--------6/01 code=L (b?)
2a | 4/23------------a-----------5/20
2b | 5/07------------b-----------6/03
------+------------+------------------------------+-------------------------------+------------------------------+----------
3 | 5/23--first---6/05 code=L (a?)
3a | 5/21------------a-----------6/17
3b | 6/04------------b-----------7/01
------+------------+------------------------------+-------------------------------+------------------------------+----------
1 | 4/10-------first--------5/03 code=b
1a | 3/26-----------a------------4/22
1b | 4/09-----------b------------5/06
------+------------+------------------------------+-------------------------------+------------------------------+----------
Month |3(March) 4(April) 5(May) 6(June) 7(Jury)
------+------------+------------------------------+-------------------------------+------------------------------+----------
Date |2 33 0 1 2 3 0 1 2 33 0 1 2 3 0 1
|012345678901 123456789012345678901234567890 1234567890123456789012345678901 123456789012345678901234567890 1234567890
------+------------+------------------------------+-------------------------------+------------------------------+----------
I thought that the query would be like:
Code:
SELECT ft.*
, CASE
WHEN ft.start_date >= sta.start_date
AND ft.end_date <= sta.end_date THEN
'a'
WHEN ..... THEN
'b'
WHEN ..... THEN
'c'
END AS code
FROM first_table ft
JOIN second_table sta
ON sta.period = ft.period
AND sta.code = 'a'
JOIN second_table stb
ON stb.period = ft.period
AND stb.code = 'b'
;