MySQL 合并非空值与其他 case when 和合并语句冲突 - 不确定如何解决

MySQL coalesce non-null values conflicting with additional case when and coalesce statements - not sure how to fix

我无法将我编写的最初忽略空值的代码转换为接受空值并将其转换为 0 的代码。

where 子句删除 ladder_advocacy = null 的任何值。我们还在 case 语句中利用它。

但是,我实际上很关心 ladder_advocacy (Etc) 何时为空。当一个人第一次加入组织时,他们的默认阶梯值是空的。在我的分析中,我想知道有多少人从 A→B、B→E 等以及 null -> B、null -> e 移动。现在,查询严重低估了任何变化,因为我们知道有一定比例的人实际上并不是首先在阶梯级别(比如作为 C1)加入组织,而是首先作为空加入。

我不确定如何解决这个问题。

这是原代码:

CREATE TEMPORARY TABLE cte_leenk_ladder_history_order
     SELECT
        lh.member_id
        ,sc.salesforce_id
        ,sc.lastname
        ,trigger_record_id
        ,ladder_change
        ,date_trigger_event
        ,@ladder_value
        ,lh.ladder_config_id AS ladder_config_id
        ,trigger_name
        ,lc.active as config_active
        ,lc.`trigger` as trigger_active
        ,COALESCE(lh.ladder_advocacy,lh.ladder_elected,lh.ladder_policy,lh.ladder_organizing,lh.ladder_organizing,lh.ladder_engagement) AS ladder_value
        ,CASE WHEN lh.ladder_advocacy IS NOT NULL THEN 'ladder_advocacy'
               WHEN lh.ladder_elected IS NOT NULL THEN 'ladder_elected'
               WHEN lh.ladder_policy IS NOT NULL THEN 'ladder_policy'
               WHEN lh.ladder_organizing IS NOT NULL THEN 'ladder_organizing'
               WHEN lh.ladder_collective IS NOT NULL THEN 'ladder_collective'
               WHEN lh.ladder_engagement IS NOT NULL THEN 'ladder_engagement'
            END
            AS ladder_name
        FROM leeds_new.leenk_ladder_history AS lh
            INNER JOIN leeds_new.leenk_ladder_config AS lc ON lh.ladder_config_id = lc.ladder_config_id
            INNER JOIN leeds_new.salesforce_contacts AS sc ON lh.member_id = sc.salesforce_id
WHERE lh.ladder_advocacy IS NOT NULL
ORDER BY date_trigger_event  DESC;

我尝试在第一个 table 之前的临时 table 中执行一个 coalese 函数来转换 null –> 0,然后将其加入:

CREATE TEMPORARY TABLE cte_ladder_levels_to_non_null
                SELECT
                 member_id,
                COALESCE(ladder_advocacy, 0) AS ladder_advocacy,
                COALESCE(ladder_elected, 0) AS ladder_elected,
                COALESCE(ladder_policy, 0) AS ladder_policy,
                COALESCE(ladder_organizing, 0) AS ladder_organizing,
                COALESCE(ladder_engagement, 0) AS ladder_engagement,
                COALESCE(ladder_collective, 0) AS ladder_collective
    FROM
         leeds_new.leenk_ladder_history;

CREATE TEMPORARY TABLE cte_leenk_ladder_history_order
     SELECT
        lh.member_id
        ,sc.salesforce_id
        ,sc.lastname
        ,trigger_record_id
        ,ladder_change
        ,date_trigger_event
        ,@ladder_value
        ,lh.ladder_config_id AS ladder_config_id
        ,trigger_name
        ,lc.active as config_active
        ,lc.`trigger` as trigger_active
        ,COALESCE(
                cte_ladder_levels_to_non_null.ladder_advocacy,
                cte_ladder_levels_to_non_null.ladder_elected,
                cte_ladder_levels_to_non_null.ladder_policy,
                cte_ladder_levels_to_non_null.ladder_organizing,
                cte_ladder_levels_to_non_null.ladder_organizing,
                cte_ladder_levels_to_non_null.ladder_engagement) AS ladder_value
        ,CASE WHEN lh.ladder_advocacy IS NOT NULL THEN 'ladder_advocacy'
               WHEN lh.ladder_elected IS NOT NULL THEN 'ladder_elected'
               WHEN lh.ladder_policy IS NOT NULL THEN 'ladder_policy'
              WHEN lh.ladder_organizing IS NOT NULL THEN 'ladder_organizing'
               WHEN lh.ladder_collective IS NOT NULL THEN 'ladder_collective'
               WHEN lh.ladder_engagement IS NOT NULL THEN 'ladder_engagement'
            END
            AS ladder_name
        FROM leeds_new.leenk_ladder_history AS lh
inner join cte_ladder_levels_to_non_null as llnn on lh.member_id = llnn.member_id
            INNER JOIN leeds_new.leenk_ladder_config AS lc ON lh.ladder_config_id = lc.ladder_config_id
            INNER JOIN leeds_new.salesforce_contacts AS sc ON lh.member_id = sc.salesforce_id
WHERE lh.ladder_advocacy IS NOT NULL
ORDER BY date_trigger_event  DESC;

但是我在这一行中收到“无效字段名称”错误:cte_ladder_levels_to_non_null.ladder_advocacy(以及后面的阶梯级别)。

我也试过删除 where 子句,但这也没有解决问题,因为它搞砸了。

想法?谢谢!

将 ifnull 移动到查询的后半部分(最后一个永久 table):

ifnull(member_id_lh2,0), 
            ifnull(trigger_name_lh2,0),
            ifnull(trigger_record_id_lh2,0), 
            date_trigger_event_lh2,
            ifnull(ladder_name_lh2,0),
            ifnull(ladder_value_lh2,0),
            ifnull(ladder_change_lh2,0),
            ifnull(config_active_lh2,0),
            ifnull(trigger_active_lh2,0),

完美运行。