This is a little more complicated than the usual how do I display records from one table which are not in the other.
Rather, each record from one table should appear 'x' number of times in the other.
The table definitions and data are as follows:
CREATE TABLE `time_code` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`AREA_ID` bigint NOT NULL,
`TIME_CODE` varchar(10) NOT NULL,
`DESCRIPTION` varchar(255) NOT NULL,
`FISCAL_YEAR` bigint NOT NULL,
PRIMARY KEY (`ID`)
)
Id, Area, Timecode, Description, Fiscal Year
1, 51, 101, "Project A", 2022
2, 51, 102, "Project B", 2022
CREATE TABLE `schedule_data` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`AREA_ID` bigint NOT NULL,
`TERRITORY_ID` bigint NOT NULL,
`GROUP_ID` bigint NOT NULL,
`TIMECODE_ID` bigint NOT NULL,
`CALENDAR_MONTH` bigint NOT NULL,
`FISCAL_YEAR` bigint not null,
PRIMARY KEY (`ID`)
)
Id, Area, Territory, Group, Timecode ID, Month, Fiscal Year
1, 51, 52, 53, 1, 2, 2022
2, 51, 52, 54, 1, 2, 2022
3, 51, 52, 55, 1, 2, 2022
4, 51, 52, 53, 2, 2, 2022
In this example, time code 101 appears in each of the groups (53, 54, and 55). Time code 102 appears only in group 53. It is missing from groups 54 and 55.
Each timecode must appear in the schedule_data table for each group (3 times, but it could vary depending upon the number of groups)
How do I write a query that tells me which groups are missing time codes?
Time codes apply to an entire area and the code must exist exactly once for each group...even if the values being tracked are zero.
Edit to add org code table and sample records:
// adjacency list model
CREATE TABLE `organization_map` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`ORG_CODE` bigint NOT NULL,
`NAME` varchar(255) NOT NULL,
`PARENT_ID` bigint NOT NULL,
`FISCAL_YEAR` bigint NOT NULL,
PRIMARY KEY (`ID`)
)
Id, Org Code, Name, Parent Id, Fiscal Year
51, 1, "Area 1", 0, 2022
52, 1, "Territory 1", 51, 2022
53, 5, "Group 1", 52, 2022
54, 6, "Group 2", 52, 2022
55, 7, "Group 3", 52, 2022
56, 2, "Territory 2", 51, 2022
57, 14, "Group 4", 56, 2022
58, 15, "Group 5", 56, 2022