无法使用SQL / Bigquery JOIN获取这两个记录

时间:2014-12-15 15:17:03

标签: sql google-bigquery

我有一个返回订单详细信息的查询,我想从briisk表中获取已找到的交易的信息。即使briisk表没有任何内容,我也希望它显示订单 如果我添加最后一行(和flostream.briisk.master = ""),我的查询只返回一个结果而不是两个。

SELECT *
FROM (SELECT orderno,ifnull(dealid,sales_rule) as DealIDCombo from flostream.orders left join  mobileheads.surveys on mobileheads.surveys.order_number = flostream.orders.externalreference) as first
INNER JOIN flostream.orders on first.orderno = flostream.orders.orderno
LEFT JOIN flostream.briisk on first.dealidcombo = flostream.briisk.uniquereference
WHERE first.orderno in (359692,359683)
//AND flostream.briisk.master = ""

1 个答案:

答案 0 :(得分:2)

当您使用left outer join时,您需要在on子句的第二个表中包含过滤条件。所以试试这个:

SELECT *
FROM (SELECT orderno,ifnull(dealid,sales_rule) as DealIDCombo
      from flostream.orders left join
           mobileheads.surveys
           on mobileheads.surveys.order_number = flostream.orders.externalreference
     ) as first INNER JOIN
     flostream.orders
     on first.orderno = flostream.orders.orderno LEFT JOIN
        flostream.briisk
     on first.dealidcombo = flostream.briisk.uniquereference AND
        flostream.briisk.master = ""
WHERE first.orderno in (359692, 359683)

第一个表的条件应该放在WHERE子句中。