SELECT g.g                                   AS "Date",
       COALESCE(t."CrossConversionCount", 0) AS "CrossConversionCount",
       COALESCE(t."Clicks", 0)               AS "Clicks",
       COALESCE(t."ImpressionCount", 0)      AS "ImpressionCount",
       COALESCE(t."ApplicationCount", 0)     AS "ApplicationCount",
       COALESCE(t."LeadCount", 0)            AS "LeadCount",
       COALESCE(t."HoldCount", 0)            AS "HoldCount",
       COALESCE(t."SaleCount", 0)            AS "SaleCount",
       COALESCE(t."RejectCount", 0)          AS "RejectCount",
       COALESCE(t."CalculationCount", 0)     AS "CalculationCount",
       COALESCE(t."ApprovementCount", 0)     AS "ApprovementCount",
       COALESCE(t."RewardAmount", 0.0)       AS "RewardAmount",
       COALESCE(t."SravniRevenue", 0.0)      AS "SravniRevenue",
       COALESCE(t."Margin", 0.0)             AS "Margin"
FROM generate_series('2024-09-01'::timestamp , '2024-11-21'::timestamp , '1 day'::interval) AS g
         LEFT JOIN (SELECT date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')        AS "Date",
                           (SELECT COUNT(CASE
                                             WHEN (e1.parent_conversion_reward_id IS NOT NULL) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e1
                            WHERE (((e1.datetime >= '2024-09-01') AND (e1.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                              FROM mart.event_cpa AS e2
                                                                                              WHERE e1.conversion_reward_id = e2.parent_conversion_reward_id) >
                                                                                             0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e1.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e1.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)))))  AS "CrossConversionCount",
                           (SELECT COUNT(CASE
                                             WHEN (e4.data_source_id IN (3, 1) AND (e4.action_type = 'click')) AND
                                                  (e4.is_unique = TRUE) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e4
                            WHERE (((e4.datetime >= '2024-09-01') AND (e4.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                              FROM mart.event_cpa AS e5
                                                                                              WHERE e4.conversion_reward_id = e5.parent_conversion_reward_id) >
                                                                                             0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e4.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e4.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)))))  AS "Clicks",
                           (SELECT COUNT(CASE
                                             WHEN e7.data_source_id IN (3, 2) AND (e7.action_type = 'impression') THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e7
                            WHERE (((e7.datetime >= '2024-09-01') AND (e7.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                              FROM mart.event_cpa AS e8
                                                                                              WHERE e7.conversion_reward_id = e8.parent_conversion_reward_id) >
                                                                                             0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e7.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e7.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)))))  AS "ImpressionCount",
                           (SELECT COUNT(CASE
                                             WHEN ((e10.data_source_id = 5) AND (e10.processing_status = 2)) AND
                                                  (((e10.step_id IS NULL)) OR (e10.step_id > 0)) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e10
                            WHERE (((e10.datetime >= '2024-09-01') AND (e10.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e11
                                                                                                WHERE e10.conversion_reward_id = e11.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e10.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e10.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "ApplicationCount",
                           (SELECT COUNT(CASE
                                             WHEN ((e13.data_source_id = 5) AND ((e13.offer_id IS NOT NULL))) AND
                                                  (((((e13.step_id IS NULL)) OR (e13.step_id > 0)) OR
                                                    (e13.processing_status = 2)) OR
                                                   ((e13.affiliate_tariff_id IS NOT NULL))) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e13
                            WHERE (((e13.datetime >= '2024-09-01') AND (e13.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e14
                                                                                                WHERE e13.conversion_reward_id = e14.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e13.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e13.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "LeadCount",
                           (SELECT COUNT(CASE
                                             WHEN ((e16.data_source_id = 5) AND ((e16.processing_status = 2) OR
                                                                                 ((e16.processing_status = 0) AND (e16.step_id = 1)))) AND
                                                  (((e16.conversion_external_status = 0) AND
                                                    ((e16.conversion_status <> 2) OR ((e16.conversion_status IS NULL)))) OR
                                                   (((e16.conversion_external_status <> 2) OR
                                                     ((e16.conversion_external_status IS NULL))) AND
                                                    (e16.conversion_status = 4))) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e16
                            WHERE (((e16.datetime >= '2024-09-01') AND (e16.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e17
                                                                                                WHERE e16.conversion_reward_id = e17.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e16.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e16.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "HoldCount",
                           (SELECT COUNT(CASE
                                             WHEN (((e19.data_source_id = 5) AND (e19.processing_status = 2)) AND
                                                   (e19.conversion_status = 1)) AND (e19.conversion_external_status = 1)
                                                 THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e19
                            WHERE (((e19.datetime >= '2024-09-01') AND (e19.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e20
                                                                                                WHERE e19.conversion_reward_id = e20.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e19.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e19.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "SaleCount",
                           (SELECT COUNT(CASE
                                             WHEN ((e22.data_source_id = 5) AND (((e22.step_id > 0) OR
                                                                                  (((e22.step_id IS NULL)) AND (e22.processing_status = 1))) OR
                                                                                 (e22.processing_status = 2))) AND
                                                  (((e22.conversion_status = 2) OR (e22.conversion_external_status = 2)) OR
                                                   (e22.processing_status = 1)) THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e22
                            WHERE (((e22.datetime >= '2024-09-01') AND (e22.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e23
                                                                                                WHERE e22.conversion_reward_id = e23.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e22.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e22.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "RejectCount",
                           (SELECT COUNT(CASE
                                             WHEN (((e25.data_source_id = 6) AND
                                                    (((e25.offer_dependency_type & 4) <> 4) OR
                                                     ((e25.offer_dependency_type IS NULL)))) AND
                                                   ((e25.is_conversion_concealing = FALSE) OR
                                                    ((e25.is_conversion_concealing IS NULL)))) AND (e25.step_id = 5)
                                                 THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e25
                            WHERE (((e25.datetime >= '2024-09-01') AND (e25.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e26
                                                                                                WHERE e25.conversion_reward_id = e26.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e25.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e25.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "CalculationCount",
                           (SELECT COUNT(CASE
                                             WHEN (((e28.data_source_id = 6) AND
                                                    (((e28.offer_dependency_type & 4) <> 4) OR
                                                     ((e28.offer_dependency_type IS NULL)))) AND
                                                   ((e28.is_conversion_concealing = FALSE) OR
                                                    ((e28.is_conversion_concealing IS NULL)))) AND (e28.step_id = 6)
                                                 THEN 1
                               END)::INT
                            FROM mart.event_cpa AS e28
                            WHERE (((e28.datetime >= '2024-09-01') AND (e28.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e29
                                                                                                WHERE e28.conversion_reward_id = e29.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e28.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e28.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "ApprovementCount",
                           (SELECT COALESCE(SUM(CASE
                                                    WHEN ((((e31.data_source_id = 5) AND (e31.processing_status = 2)) AND
                                                           (e31.conversion_status = 1)) AND
                                                          (e31.conversion_external_status = 1)) AND
                                                         ((e31.reward_amount IS NOT NULL)) THEN e31.reward_amount
                               END), 0.0)
                            FROM mart.event_cpa AS e31
                            WHERE (((e31.datetime >= '2024-09-01') AND (e31.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e32
                                                                                                WHERE e31.conversion_reward_id = e32.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e31.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e31.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "RewardAmount",
                           (SELECT COALESCE(SUM(CASE
                                                    WHEN ((((e34.data_source_id = 5) AND (e34.processing_status = 2)) AND
                                                           (e34.conversion_status = 1)) AND
                                                          (e34.conversion_external_status = 1)) AND
                                                         ((e34.sravni_revenue IS NOT NULL)) THEN e34.sravni_revenue
                               END), 0.0)
                            FROM mart.event_cpa AS e34
                            WHERE (((e34.datetime >= '2024-09-01') AND (e34.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e35
                                                                                                WHERE e34.conversion_reward_id = e35.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e34.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e34.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "SravniRevenue",
                           (SELECT COALESCE(SUM(CASE
                                                    WHEN ((((e37.data_source_id = 5) AND (e37.processing_status = 2)) AND
                                                           (e37.conversion_status = 1)) AND
                                                          (e37.conversion_external_status = 1)) AND
                                                         ((e37.sravni_revenue IS NOT NULL)) THEN e37.sravni_revenue
                               END), 0.0)
                            FROM mart.event_cpa AS e37
                            WHERE (((e37.datetime >= '2024-09-01') AND (e37.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e38
                                                                                                WHERE e37.conversion_reward_id = e38.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e37.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e37.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) -
                           (SELECT COALESCE(SUM(CASE
                                                    WHEN ((((e40.data_source_id = 5) AND (e40.processing_status = 2)) AND
                                                           (e40.conversion_status = 1)) AND
                                                          (e40.conversion_external_status = 1)) AND
                                                         ((e40.reward_amount IS NOT NULL)) THEN e40.reward_amount
                               END), 0.0)
                            FROM mart.event_cpa AS e40
                            WHERE (((e40.datetime >= '2024-09-01') AND (e40.datetime < '2024-11-20')) AND ((SELECT COUNT(*)::INT
                                                                                                FROM mart.event_cpa AS e41
                                                                                                WHERE e40.conversion_reward_id = e41.parent_conversion_reward_id) >
                                                                                               0))
                              AND ((date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC') =
                                    date_trunc('day', (e40.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) OR
                                   ((((e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL)) AND
                                    (((e40.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC' IS NULL))))) AS "Margin"
                    FROM mart.event_cpa AS e
                    WHERE ((e.datetime >= '2024-09-01') AND (e.datetime < '2024-11-20'))
                      AND ((SELECT COUNT(*)::INT
                            FROM mart.event_cpa AS e0
                            WHERE e.conversion_reward_id = e0.parent_conversion_reward_id) > 0)
                    GROUP BY date_trunc('day', (e.datetime + INTERVAL '3 hours') AT TIME ZONE 'UTC')) AS t
                   ON g.g = t."Date"
ORDER BY g.g DESC

e.datetime - outer query column, how to fix it?