代码之家  ›  专栏  ›  技术社区  ›  civesuas_sine

oracle按警告解码组

  •  2
  • civesuas_sine  · 技术社区  · 7 年前

    我有这个密码

    for x_eo in ( select decode(mod(card_name_id,2),0,1,1,2) e_o, count(*) nr 
                            from  rp_Deck where session_id=p_session_id_in 
                           and position<=35 group by mod(card_name_id,2) )
    

    我收到sqldeveloper警告,select列表与group by不一致。 开发人员为我提供了解决方案:

    select decode(mod(card_name_id,2),0,1,1,2) e_o, count(*) nr 
                            from  rp_Deck where session_id=p_session_id_in 
                           and position<=35 group by mod(card_name_id,2), card_name_id, 2, decode(mod(card_name_id,2),0,1,1,2) )
    

    这两组人的区别是什么? 谢谢

    1 回复  |  直到 7 年前
        1
  •  4
  •   MT0    7 年前

    通常,当您使用 GROUP BY 在语句中,所有值都需要为:

    • 常数;
    • 在聚合功能内;或
    • 分组依据 条款

    SQL开发人员没有意识到这一点 decode(value_mod_2,0,1,1,2) 实际上只是将1添加到值中,并且不会更改项目对组的分配,因此,由于它不是常量或聚合函数,因此它希望整个函数都位于 分组依据 条款

    我个人认为:

    select mod(card_name_id,2) + 1 e_o,
           count(*) nr 
    from   rp_Deck
    where  session_id=p_session_id_in 
    and    position<=35
    group by mod(card_name_id,2)
    

    (the + 1 是常量,因此不需要在 分组依据 条款)

    SQL开发人员提出的解决方案错误如下:

    select decode(mod(card_name_id,2),0,1,1,2) e_o,
           count(*) nr 
    from   rp_Deck
    where  session_id=p_session_id_in 
    and    position<=35
    group by
           mod(card_name_id,2),
           card_name_id,
           2,
           decode(mod(card_name_id,2),0,1,1,2)
    

    实际上与按最细粒度分组相同,因此:

    group by card_name_id;
    

    这不是您想要分组的依据。为了与原始查询的预期输出相同,它应该提出如下建议:

    group by
           mod(card_name_id,2),
           decode(mod(card_name_id,2),0,1,1,2)
    

    或者更简单地说:

    group by
           decode(mod(card_name_id,2),0,1,1,2)