语法错误在或附近"当"

时间:2018-01-17 15:14:21

标签: sql postgresql

这应该很简单。

  

"语法错误在或附近"当""第5行错误

    round(CAST(CASE WHEN x.sprd_units = 0 then 0 ELSE 
        case 
        when sum(case when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') 
            THEN d.unit_qty else 0 end) / z.spread_units_cb::numeric
        when sum(case when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt not in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') 
            THEN d.unit_qty else 0 end) / x.sprd_units::numeric
        end as numeric) 2) as space_units,   

Postgresql版本是8.3

我错过了什么或什么?提前致谢

3 个答案:

答案 0 :(得分:2)

你在这里发生了大量的嵌套,还有一些括号用于启动。我处理像这样的混乱的方式就是像一个疯狂的人一样缩进它。你很快就会发现你错过了一些与某些WHEN条款匹配的THEN条款,而那些相同的WHEN条款缺少条件。你只是在划分某些东西(而不是比较某些东西)。最后,您错过了一个CASE语句的END。

round(
    CAST(
        CASE 
            WHEN x.sprd_units = 0 
                then 0 
            ELSE 
                case 
                    when sum(
                                case 
                                    when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') 
                                        THEN d.unit_qty 
                                    else 0 
                                    end
                            ) / z.spread_units_cb::numeric
                        /*WHEN this is what? there is no condition here, just math*/
                        /*WHERE IS THE THEN ?*/
                    when sum(
                                case 
                                    when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt not in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') 
                                        THEN d.unit_qty 
                                    else 0 
                                    end
                            ) / x.sprd_units::numeric
                        /*WHEN this is what? there is no condition here, just math*/
                        /*AGAIN NO THEN*/
                    /*THIS CASE IS MISSING AN END*/
        end as numeric
    ), 2
) as space_units, 

答案 1 :(得分:1)

所有这些嵌套都是不需要的。 CASE支持多种条件。您缺少两个CASE条件(下面标有?),但我假设第二个是ELSE。您在" 2":

之前也缺少逗号
round(cast(case when x.sprd_units = 0 then 0 
                when ?
                then sum(case when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt in ('CORE BROCHURE', 'CORE BROCHURE ATTACHED', 'CORE BROCHURE UNATTACHED') 
                              then d.unit_qty
                              else 0
                         end) / z.spread_units_cb::numeric
                else sum(case when d.ver_desc_txt = 'BEST OFFICIAL PROJECTION' AND d.veh_desc_txt not in ('CORE BROCHURE', 'CORE BROCHURE ATTACHED', 'CORE BROCHURE UNATTACHED') 
                              then d.unit_qty else 0
                         end) / x.sprd_units::numeric
           end as numeric), 2) as space_units,   

答案 2 :(得分:0)

我终于做到了:

round(CAST(sum(d.unit_qty / case 
                                    when d.veh_desc_txt in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') and d.ver_desc_txt = 'BEST OFFICIAL PROJECTION'
                                        then nullif(z.spread_units_cb, 0)::numeric
                                    when d.veh_desc_txt not in ('CORE BROCHURE','CORE BROCHURE ATTACHED','CORE BROCHURE UNATTACHED') and d.ver_desc_txt = 'BEST OFFICIAL PROJECTION'
                                        then nullif(x.sprd_units, 0)::numeric

                                    end)
            as numeric),                        
        2) as space_units,