我正在尝试运行这样的UNION查询:
#standardSQL
SELECT
s.campaign_name,
date(event_date) as event_date,
'email' medium,
count(s.send_id) sends
from pixel_logs.table1 s
inner join pixel_logs.full_logs AS l on l.uid = s.uid
where 1=1
group by
event_date,
campaign_name
order by
event_date
UNION ALL
select
s.campaign_name,
date(event_date) as event_date,
'sms' medium,
count(s.send_id)
from pixel_logs.table2 s
inner join pixel_logs.full_logs AS l on l.uid = s.uid
where 1=1
and account_id = 'xxxx'
group by
event_date,
campaign_name
order by
event_date;
但是我收到了这条消息:
Syntax error: Unexpected keyword UNION at [21:1] Dismiss
如何解决这个问题?
答案 0 :(得分:4)
在UNION
个查询ORDER BY
通常在最后应用一次,而不是查询的每个部分。您可以尝试从查询的前半部分删除ORDER BY
:
#standardSQL
select
s.campaign_name,
date(event_date) as event_date,
'email' medium,
count(s.send_id) sends
from pixel_logs.table1 s
inner join pixel_logs.full_logs AS l on l.uid = s.uid
where 1=1
group by
event_date,
campaign_name
UNION ALL
select
s.campaign_name,
date(event_date)
'sms' medium,
count(s.send_id)
from pixel_logs.table2 s
inner join pixel_logs.full_logs AS l on l.uid = s.uid
where 1=1
and account_id = 'xxxx'
group by
event_date,
campaign_name
order by
event_date;
此外,在UNION
查询的后半部分中不应该需要任何别名,因为前半部分中指定的别名是将要使用的别名。