简体   繁体   English

Sql Bigquery - 将两列连接成一个新列

[英]Sql Bigquery - concat two columns into a new column

I want to concat two columns shown below into one new column.我想将下面显示的两列合并为一个新列。

A_Bill_ID B_BILL_ID

I'm using this query:我正在使用这个查询:

SELECT
CONCAT( A_Bill_ID , ' ', B_BILL_ID ) AS BILL_ID
FROM
`table.tmp_140222`

From this query, the result will be like this.从这个查询中,结果将是这样的。

在此处输入图像描述

My problem is I want to concat only columns that don't have a similar value so that the new column value will not duplicate two times.我的问题是我只想连接没有相似值的列,这样新的列值就不会重复两次。 From the above result, the ID is duplicated since both of the columns has the same value.从上面的结果来看,ID 是重复的,因为两列都具有相同的值。

Can anyone help me?谁能帮我?

Thanks谢谢

select if (
    a_bill_id = b_bill_id, 
    a_bill_id, 
    concat(a_bill_id , ' ', b_bill_id )
  ) as bill_id
from your_table    

... shows null value.... ... 显示 null 值....

select if (
    a_bill_id = b_bill_id or (a_bill_id = b_bill_id) is null,
    coalesce(a_bill_id, b_bill_id),
    concat(a_bill_id , ' ', b_bill_id )
  ) as bill_id
from your_table

You can use a case statement like this -您可以使用这样的案例陈述 -

SELECT
case when A_Bill_ID = B_BILL_ID then a_bill_Id 
else CONCAT(A_Bill_ID , ' ', B_BILL_ID ) 
end AS BILL_ID
FROM `table.tmp_140222`

If there are Null values in the initial columns, you can use IFNULL or COALEASE to replace that with an empty string.如果初始列中有 Null 个值,您可以使用 IFNULL 或 COALEASE 将其替换为空字符串。 But in this case, I would suggest using this without a delimiter or writing multiple 'when' statements to avoid tailing spaces in the final column -但在这种情况下,我建议在没有分隔符的情况下使用它或编写多个“when”语句以避免在最后一列中出现尾部空格 -

SELECT
case when A_Bill_ID = B_BILL_ID then a_bill_Id 
else CONCAT(IFNULL(A_Bill_ID), ''), "", IFNULL(B_BILL_ID, ''))
end AS BILL_ID
FROM `table.tmp_140222`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM