Forenkling af syntaks
Er der nogen der har overblik og skud til at se på om denne syntaks kan forenkles?SELECT *
FROM SITE JOIN SITE_EVENTS AS site_events_a
ON SITE.SITE_SEQ_NO = site_events_a.SITE_SEQ_NO
JOIN EVENT_TYPES AS event_types_a
ON site_events_a.EVENT_TYPE_ID = event_types_a.EVENT_TYPE_ID
JOIN SITE_EVENTS AS site_events_b
ON SITE.SITE_SEQ_NO = site_events_b.SITE_SEQ_NO
JOIN EVENT_TYPES AS event_types_b
ON site_events_b.EVENT_TYPE_ID = event_types_b.EVENT_TYPE_ID
JOIN SITE_EVENTS AS site_events_c
ON SITE.SITE_SEQ_NO = site_events_c.SITE_SEQ_NO
JOIN EVENT_TYPES AS event_types_c
ON site_events_c.EVENT_TYPE_ID = event_types_c.EVENT_TYPE_ID
JOIN SITE_EVENTS AS site_events_d
ON SITE.SITE_SEQ_NO = site_events_d.SITE_SEQ_NO
JOIN EVENT_TYPES AS event_types_d
ON site_events_d.EVENT_TYPE_ID = event_types_d.EVENT_TYPE_ID
WHERE site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.210'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_c.EVENT_END_PERIOD
AND site_events_c.EVENT_END_PERIOD = site_events_d.EVENT_END_PERIOD
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.210'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_c.EVENT_END_PERIOD
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '21.005'
AND EVENT_END_PERIOD LIKE '%pulje%')
)
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_d.EVENT_ORDERED_BY LIKE '%Region%'
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_d.EVENT_END_PERIOD
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '21.210'
AND EVENT_END_PERIOD LIKE '%pulje%')
)
AND site_events_c.EVENT_ORDERED_BY LIKE '%Region%'
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_d.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_c.EVENT_ORDERED_BY LIKE '%Region%'
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID IN ('21.210','21.005')
AND EVENT_END_PERIOD LIKE '%pulje%')
)
OR
site_events_a.EVENT_TYPE_ID = site_events_b.EVENT_TYPE_ID
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.210'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD LIKE '%light%'
AND site_events_c.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_d.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '50.021')
)
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.210'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_b.EVENT_EXECUTED_BY LIKE '%Region%'
AND site_events_c.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND site_events_d.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
OR
site_events_a.EVENT_TYPE_ID = site_events_b.EVENT_TYPE_ID
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.210'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_b.EVENT_EXECUTED_BY = '%Region%'
AND site_events_c.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND site_events_d.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '50.021')
)
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.208'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_c.EVENT_END_PERIOD
AND site_events_c.EVENT_END_PERIOD = site_events_d.EVENT_END_PERIOD
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.208'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_c.EVENT_END_PERIOD
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_d.EVENT_ORDERED_BY LIKE '%Region%'
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '21.005'
AND EVENT_END_PERIOD LIKE '%pulje%')
)
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD = site_events_d.EVENT_END_PERIOD
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_c.EVENT_ORDERED_BY LIKE '%Region%'
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '21.208'
AND EVENT_END_PERIOD LIKE '%pulje%')
)
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_d.EVENT_ORDERED_BY LIKE '%Region%'
AND site_events_c.EVENT_ORDERED_BY LIKE '%Region%'
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID IN ('21.208','21.005')
AND EVENT_END_PERIOD LIKE '%pulje%')
)
OR
site_events_a.EVENT_TYPE_ID = site_events_b.EVENT_TYPE_ID
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.208'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_a.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_b.EVENT_END_PERIOD LIKE '%light%'
AND site_events_c.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND site_events_d.EVENT_END_PERIOD = site_events_b.EVENT_END_PERIOD
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '50.021')
)
OR
site_events_a.EVENT_TYPE_ID = '50.021'
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.208'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_b.EVENT_EXECUTED_BY LIKE '%Region%'
AND site_events_c.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND site_events_d.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
OR
site_events_a.EVENT_TYPE_ID = site_events_b.EVENT_TYPE_ID
AND site_events_b.EVENT_TYPE_ID = '21.001'
AND site_events_c.EVENT_TYPE_ID = '21.208'
AND site_events_d.EVENT_TYPE_ID = '21.005'
AND site_events_b.EVENT_EXECUTED_BY = '%Region%'
AND site_events_c.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND site_events_d.EVENT_EXECUTED_BY = site_events_b.EVENT_EXECUTED_BY
AND (NOT EXISTS
(SELECT *
FROM SITE_EVENTS
WHERE SITE_SEQ_NO = SITE.SITE_SEQ_NO
AND EVENT_TYPE_ID = '50.021')
)