Table XX
Day-A Day-1 Day-B Day-2 Cat Dog
---------- ---------- ---------- ---------- ----- -----
2018/01/12 NULL NULL NULL Y N
NULL NULL 2018/02/02 NULL N Y
NULL NULL 2018/01/26 NULL N Y
NULL 2018/01/15 NULL NULL Y N
NULL NULL NULL 2018/01/15 N Y
NULL NULL 2018/01/19 NULL N Y
I would like to match Day-A and Day-B, and Day-1 and Day-2. Day-A and Day-1 will be tagged as 'Cat', while Day-B and Day-2 will be tagged as 'Dog'. 'Cat' always matched with 'Dog'
- Day-A and Day-B need to be matched by day e.g. Monday = Monday.
- Day-1 and Day-2 need to be matched by same date.
I can do the Day-1 and Day-2 match, but not another one.
INSERT INTO T1 (Day-A, Day-1, Day-B, Day-2, c.Cat, d.Dog)
SELECT c.Day-A, c.Day-1, d.Day-B, d.Day-2
FROM calendar AS c
LEFT JOIN calendar AS d
ON ((IFNULL(c.Day-1, 'x') = IFNULL(d.Day-2, 'x')
AND IFNULL(DAYNAME(c.Day-A), 'x') = IFNULL(DAYNAME(d.Day-B), 'x'))
WHERE c.Cat = d.Dog
I matched Day-1 and Day-2 but not another pair. Only the closest date is showing up. In my case only 2018/01/19 is showed, but not 2018/1/26 nor 2018/02/02, though they are all Friday. Day-A and Day-B can be several months different in my table.
My desired table result:
Id Day-A Day-1 Day-B Day-2 Cat Dog
--- ---------- ---------- ---------- ---------- ----- -----
1 2018/01/12 NULL 2018/01/19 NULL Y Y
2 2018/01/12 NULL 2018/01/26 NULL Y Y
3 2018/01/12 NULL 2018/02/02 NULL Y Y
4 NULL 2018/01/15 NULL 2018/01/15 Y Y
Is it possible to have more than one result for DAYNAME() match?