与聚合一起使用UNION时出现SQL语句错误

与聚合一起使用UNION时出现SQL语句错误,sql,amazon-redshift,union,Sql,Amazon Redshift,Union,我在amazon redshift中使用的SQL在第二个select语句中出错,尽管两个查询在没有联合的情况下都是有效的: select p.date, sum(p.pageviews) as "total_pageviews", sum(p.sessions) as "total_sessions", l.location_id, p.brand_key from public.local_site_sessions p, public

我在amazon redshift中使用的SQL在第二个select语句中出错,尽管两个查询在没有联合的情况下都是有效的:

select p.date,
       sum(p.pageviews) as "total_pageviews",
       sum(p.sessions)  as "total_sessions",
       l.location_id,
       p.brand_key
from public.local_site_sessions p, public.location_map l
where l.affiliate_id = p.affiliate_id
group by p.date, p.brand_key, l.location_id order by p.date

union

select g.date,
       g.sessions as "total_pageviews",
       g.pages_per_session as "total_sessions",
       g.location_id,
       brand_key
from public.ga_lead g
group by g.date, g.brand_key, g.location_id, g.sessions, g.pages_per_session    
order by g.date

错误表明第二个select语句是语法错误?如何更正此查询?

只需在初始联合查询中添加一个外部选择即可

    Select * from
     ( Select.... union Select.... )

只需在初始联合查询中添加一个外部选择

    Select * from
     ( Select.... union Select.... )

第二个查询不需要分组依据。。您没有使用聚合函数

select p.date, sum(p.pageviews) as "total_pageviews", sum(p.sessions) as "total_sessions",l.location_id, p.brand_key
FROM public.local_site_sessions p, public.location_map l
where l.affiliate_id = p.affiliate_id
group by p.date, p.brand_key, l.location_id 

union

select g.date, g.sessions , g.pages_per_session ,g.location_id, brand_key
FROM public.ga_lead g
并且您不应该根据使用显式联接的位置使用旧的隐式联接 如果需要“订购依据”,请在最后一次选择时应用“订购依据”,并且不应在第二次选择时使用别名

select p.date, sum(p.pageviews) as "total_pageviews", sum(p.sessions) as "total_sessions",l.location_id, p.brand_key
FROM public.local_site_sessions p
INNER JOIN  public.location_map l ON  l.affiliate_id = p.affiliate_id
group by p.date, p.brand_key, l.location_id 

union

select g.date, g.sessions , g.pages_per_session ,g.location_id, brand_key
FROM public.ga_lead g
order by 1,4,5 

第二个查询不需要分组依据。。您没有使用聚合函数

select p.date, sum(p.pageviews) as "total_pageviews", sum(p.sessions) as "total_sessions",l.location_id, p.brand_key
FROM public.local_site_sessions p, public.location_map l
where l.affiliate_id = p.affiliate_id
group by p.date, p.brand_key, l.location_id 

union

select g.date, g.sessions , g.pages_per_session ,g.location_id, brand_key
FROM public.ga_lead g
并且您不应该根据使用显式联接的位置使用旧的隐式联接 如果需要“订购依据”,请在最后一次选择时应用“订购依据”,并且不应在第二次选择时使用别名

select p.date, sum(p.pageviews) as "total_pageviews", sum(p.sessions) as "total_sessions",l.location_id, p.brand_key
FROM public.local_site_sessions p
INNER JOIN  public.location_map l ON  l.affiliate_id = p.affiliate_id
group by p.date, p.brand_key, l.location_id 

union

select g.date, g.sessions , g.pages_per_session ,g.location_id, brand_key
FROM public.ga_lead g
order by 1,4,5 

用您收到的错误消息编辑您的问题?-在ANSI-92 SQL标准(超过25年前)中,旧样式的逗号分隔表列表样式已被正确的ANSI
JOIN
语法所取代,因此不鼓励使用它。您收到的错误消息是否与您的问题有关在ANSI-92 SQL标准中(超过25年前),用正确的ANSI
JOIN
语法替换了老式的逗号分隔表列表样式它的使用是不鼓励的请发布您通过控制台获得的确切错误可能有光标指向该确切语法错误,如near
语法上述内容类似于我发布的运行查询的替代方法请发布您通过控制台获得的确切错误可能有光标指向该确切语法错误,如在
语法附近,上述语法类似于我发布的另一种运行查询的方法。当我们在union的查询中添加order by/group by时,我们会出现这种语法错误,主要是因为order by可能会成为union列的歧义together@HimanshuAhuja . 然后,应在末尾应用联合订货。。你的评论不清楚。。总之,我猜当我们在union的查询中添加order by/group by时,我们会出现这种语法错误,主要是因为order by可能会成为union列的歧义together@HimanshuAhuja . 然后,应在末尾应用联合订货。。你的评论不清楚。。无论如何