I have two tables:
Table 1:
Id
1232
1344
1313
4242
3242
555
Table 2:
Id sym mnth code
1232 9 1 32
1344 15 1 14
1313 10 1 32
4242 11 1 32
3242 9 1 32
1232 9 2 32
1344 13 2 14
1313 9 2 32
4242 10 2 32
3242 9 2 32
I want to check if all the id's in table 1 have the value 9 in sym for all the month (1,2 in the example) but only for those id's which for them the code in table 2 is '32'.
If not return me the id and the months for which the 9 is missing separate by a comma. If Id in table 1 doesn't exists at all in table 2 return null in the month column and the id.
The output in the example should be:
ID month
1313 1
4242 1,2
555 NULL
1344 doesn't exist because the code column for hime is not 32.
I started writing this:
SELECT table1.id
FROM table1
WHERE not EXISTS (SELECT id FROM table2
WHERE table2.sml = '9' AND table2.code = 32)
But I really don't know how to make the query run for all month and plug the results like I've mentioned in the output. Any help?
Thank you!