INSERT INTO TABLE-2( TABLE-3_ID, TABLE-1_ID,
TOGGLEFIELD)
SELECT DISTINCT 5 as
TABLE-3_ID, T1.TABLE-1_ID, 'Y' as TOGGLEFIELD FROM TABLE-1 t1, TABLE-4 t4,
TABLE-5 t5
WHERE t4.TABLE-4_ID = t1.TABLE-4_ID and t1.SOME_FIELD =
'value'
and 0 < (SELECT count(td4.TABLE-4_ID) FROM TABLE-4 td4 WHERE
td4.TABLE-4_ID = t4.TABLE-4_ID and t4.A_FIELD = 1 and td4.B_FIELD = 3)
and 0
< (SELECT count(td4.TABLE-4_ID) FROM
TABLE-4 td4 WHERE td4.TABLE-4_ID = t4.TABLE-4_ID and t4.A_FIELD = 10 and
td4.B_FIELD = 83)
and t1.TABLE-1_ID NOT IN (SELECT TABLE-1_ID from TABLE-2 where TABLE-3_ID = 5)
Alternately
INSERT INTO TABLE-2(
TABLE-3_ID, TABLE-1_ID, TOGGLEFIELD)
SELECT 5 as TABLE-3_ID, T1.TABLE-1_ID,
'Y' as TOGGLEFIELD FROM TABLE-1 t1, TABLE-4 t4, TABLE-5 t5
WHERE
t4.TABLE-4_ID = t1.TABLE-4_ID and t1.SOME_FIELD = 'value'
and 0 < (SELECT
count(td4.TABLE-4_ID) FROM TABLE-4 td4 WHERE td4.TABLE-4_ID = t4.TABLE-4_ID and
t4.A_FIELD = 1 and td4.B_FIELD = 3)
and 0 < (SELECT count(td4.TABLE-4_ID)
FROM TABLE-4 td4 WHERE td4.TABLE-4_ID = t4.TABLE-4_ID and t4.A_FIELD = 10 and
td4.B_FIELD = 83)
and t1.TABLE-1_ID NOT IN (SELECT TABLE-1_ID from TABLE-2 where TABLE-3_ID = 5)
GROUP BY
t1.TABLE-4_ID
gives the same
results
However, I can not
do any of the following because I get duplicate TABLE-1_ID values with something
like
INSERT INTO TABLE-2(
TABLE-3_ID, TABLE-1_ID, TOGGLEFIELD)
SELECT 5 as TABLE-3_ID, T1.TABLE-1_ID,
'Y' as TOGGLEFIELD FROM TABLE-1 t1, TABLE-4 t4, TABLE-5 t5
WHERE
t4.TABLE-4_ID = t1.TABLE-4_ID and t1.SOME_FIELD = 'value'
and 0 < (SELECT
count(td4.TABLE-4_ID) FROM TABLE-4 td4 WHERE td4.TABLE-4_ID = t4.TABLE-4_ID and
t4.A_FIELD = 1 and td4.B_FIELD = 3)
and 0 < (SELECT count(td4.TABLE-4_ID)
FROM TABLE-4 td4 WHERE td4.TABLE-4_ID = t4.TABLE-4_ID and t4.A_FIELD = 10 and
td4.B_FIELD = 83)
and t1.TABLE-1_ID NOT IN (SELECT TABLE-1_ID from TABLE-2 where TABLE-3_ID = 5)
GROUP BY
t1.TABLE-4_ID, t5.TABLE-5_ID
Any
suggestions?