
use d1;

SELECT
                t1.t1c1, 
                t1dp.`qac1`, 
                t2dp.`qac2`, 
                t2dp.`qac3`, 
                t2dp.`qac4`, 
                t2dp.`qac5` 
            FROM `t1` t1 
            LEFT JOIN `t2` t2 ON t2.t2c2 = t1.t1c1
            LEFT JOIN (
                SELECT
                    t3c2, 
                    SUM(IFNULL(t3c5, IFNULL(t3c4, 0))) AS `qac1` 
                FROM `t3` 
                GROUP BY t3c2
            ) AS t1dp ON t1dp.t3c2 = t1.t1c1
            LEFT JOIN (
                SELECT
                    t2.t2c2 AS t3c2_alias, 
                    SUM(IFNULL(t4.t4c6, 0)) AS `qac2`, 
                    (SUM(IFNULL(t4.t4c7, 0)) + SUM(IFNULL(t4.t4c9, 0))) AS `qac3`, 
                    SUM(IF(t2.t2c8 != 1, 1, 0)) AS `qac4`, 
                    MAX(t4.t4c10) AS `qac5` 
                FROM `t2` t2 
                JOIN `t4` t4 
                    ON t4.t4c2 = t2.t2c1
                GROUP BY t2.t2c2
            ) AS t2dp ON t2dp.t3c2_alias = t1.t1c1
            WHERE t1.t1c7 = 2
            GROUP BY t1.t1c1
            HAVING t1dp.`qac1` = t2dp.`qac2` 
                AND t2dp.`qac3` = 0 
                AND (t2dp.`qac4` = 0 
                    OR t2dp.`qac5` < '2025-11-06 11:40:00' );
