将变量添加到case语句中,然后输出

kd3sttzy  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(268)

如果我在 Impala 中有以下代码:

(CASE 
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 < a2.min2 THEN "KEEP"
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 < a2.min2 THEN "MOVE"
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 > a2.min2 THEN "MOVE"
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 > a2.min2 THEN "KEEP"
  ELSE "NOT SHIPPING"
END) AS move

我希望文本输出如下内容:

(CASE 
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 < a2.min2 THEN "KEEP"
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 < a2.min2 THEN "MOVE TO**a3.supplier**"
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 > a2.min2 THEN "MOVE TO**a4.supplier**"
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 > a2.min2 THEN "KEEP"
  ELSE "NOT SHIPPING"
END) AS move

我该如何编写代码?

sqougxex

sqougxex1#

你可以用 CONCAT() 要生成输出字符串:

(CASE 
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 < a2.min2 THEN "KEEP"
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 < a2.min2 THEN concat("MOVE TO**",coalesce(a3.supplier,''),"**")
  WHEN a3.shipped_qty1 > a4.shipped_qty2 AND a1.min1 > a2.min2 THEN concat("MOVE TO**",coalesce(a4.supplier,''),"**")
  WHEN a3.shipped_qty1 < a4.shipped_qty2 AND a1.min1 > a2.min2 THEN "KEEP"
  ELSE "NOT SHIPPING"
END) AS move

p、 我还把供应商的价值 Package 在 COALESCE() 因为我不知道这些列是否可以为null,否则如果这些值为null,查询将失败。

相关问题