尝试合并包含连接操作的2个子查询时,BigQuery union失败了

时间:2015-12-18 23:47:12

标签: google-bigquery

我们正试图联合2个子查询并获得错误。 这是查询:

select *
from
(select  TRANS_SEQ, TRANS_TS
from [X.ls_transaction] ls
left join each (select * from [X.resident_s] where date(eff_end_ts) = date('9999-12-31')) rs on ls.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) a,
(select TRANS_SEQ, TRANS_TS,
from [X.ld_transaction] ld
left join each (select * from [X.resident_d] where date(eff_end_ts) = date('9999-12-31')) rs on ld.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) b
limit 1;

这是它返回的错误:

  

错误:联盟导致模式不明确。 [TRANS_SEQ]含糊不清   是多个字段的别名。别名字段:   ld.TRANS_SEQ,ls.TRANS_SEQ,

如果我从子查询中取出两个连接,那么工会就可以工作了 这是一个错误吗?

由于

2 个答案:

答案 0 :(得分:2)

很可能你在X.resident_d和X.resident_s中有TRANS_SEQ字段,这引起了歧义。
你应该做类似下面的事情

select *
from
(select ls.TRANS_SEQ as TRANS_SEQ, TRANS_TS
from [X.ls_transaction] ls
left join each (select * from [X.resident_s] where date(eff_end_ts) = date('9999-12-31')) rs on ls.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) a,
(select ld.TRANS_SEQ as TRANS_SEQ, TRANS_TS,
from [X.ld_transaction] ld
left join each (select * from [X.resident_d] where date(eff_end_ts) = date('9999-12-31')) rs on ld.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) b
limit 1;

答案 1 :(得分:0)

尝试给两个“TRANS_SEQ”变量中的第二个赋予不同的名称并使用coalesce函数。以下是解决此问题的方法:

select coalesce (TRANS_SEQ, TRANS_SEQ_b)
from
(select  TRANS_SEQ, TRANS_TS
from [X.ls_transaction] ls
left join each (select * from [X.resident_s] where date(eff_end_ts) = date('9999-12-31')) rs on ls.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) a,
(select TRANS_SEQ as TRANS_SEQ_b , TRANS_TS,
from [X.ld_transaction] ld
left join each (select * from [X.resident_d] where date(eff_end_ts) = date('9999-12-31')) rs on ld.source_resident_key=rs.resident_key
where date(trans_ts) = date('2015-07-31')) b
limit 1;

我已经测试了它并且它有效。