将变量添加到CASE语句THEN输出中

时间:2017-02-09 17:36:53

标签: mysql hadoop impala

如果我在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

我该怎么编码?

1 个答案:

答案 0 :(得分:1)

您可以使用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.S。我还将供应商值包装在COALESCE()中,因为我不知道这些列是否为空,否则如果这些值为空,查询将失败。