加入两个相同的查询但来自不同的模式

时间:2018-05-02 20:30:13

标签: sql amazon-redshift tableau

我在Tableau中运行查询以从几个不同的表中提取电子邮件数据。现在,这起作用并产生结果。数据处于红移状态。

QUERY 1

WITH activities AS (
SELECT A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date
FROM marketo_sc.lead_activities A, marketo_sc.lead_activity_types T
WHERE T.name IN ('Send Email', 'Email Delivered', 'Open Email', 'Click Email', 'Unsubscribe Email') AND A.activity_type_id=T.id 
GROUP BY A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date
),
totals AS (
SELECT A.name AS activity_t, E.subject AS subject, E.name AS email, A.activity_date , count(*)
FROM activities A, marketo_sc.emails E
WHERE A.primary_attribute_value_id = E.id AND E.name LIKE '%Bounceback%'
GROUP BY email, activity_t, subject, A.activity_date
)
SELECT email, subject, 'sc' as program, activity_date,
sum(case when activity_t='Send Email' then count else 0 end) AS sent,
sum(case when activity_t='Email Delivered' then count else 0 end) AS delivered,
sum(case when activity_t='Open Email' then count else 0 end) AS opened,
sum(case when activity_t='Click Email' then count else 0 end) AS clicked,
sum(case when activity_t='Unsubscribe Email' then count else 0 end) AS unsubscribed
FROM totals
GROUP BY email, subject, activity_date

但是,我有另一个具有完全相同设置的表

QUERY 2

WITH tgactivities AS (
SELECT A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date
FROM marketo_tg.lead_activities A, marketo_tg.lead_activity_types T
WHERE T.name IN ('Send Email', 'Email Delivered', 'Open Email', 'Click Email', 'Unsubscribe Email') AND A.activity_type_id=T.id 
GROUP BY A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date
),
tgtotals AS (
SELECT A.name AS activity_t, E.subject AS subject, E.name AS email, A.activity_date , count(*)
FROM tgactivities A, marketo_tg.emails E
WHERE A.primary_attribute_value_id = E.id AND E.name LIKE '%Bounceback%'
GROUP BY email, activity_t, subject, A.activity_date
)
SELECT email, subject, 'tg' as program, activity_date,
sum(case when activity_t='Send Email' then count else 0 end) AS sent,
sum(case when activity_t='Email Delivered' then count else 0 end) AS delivered,
sum(case when activity_t='Open Email' then count else 0 end) AS opened,
sum(case when activity_t='Click Email' then count else 0 end) AS clicked,
sum(case when activity_t='Unsubscribe Email' then count else 0 end) AS unsubscribed
FROM tgtotals
GROUP BY email, subject, activity_date

我试图在它们之间进行UNION但是我遇到了语法错误

[Amazon] [RedShift ODBC](30)尝试执行查询时出错:错误:“WITH”处或附近的语法错误第26行:WITH tgactivities AS(^

有关为何会发生这种情况的任何帮助或想法?

1 个答案:

答案 0 :(得分:1)

SELECT email, subject, 'tg' as program, activity_date,
sum(case when activity_t='Send Email' then [count] else 0 end) AS sent,
sum(case when activity_t='Email Delivered' then [count] else 0 end) AS delivered,
sum(case when activity_t='Open Email' then [count] else 0 end) AS opened,
sum(case when activity_t='Click Email' then [count] else 0 end) AS clicked,
sum(case when activity_t='Unsubscribe Email' then [count] else 0 end) AS unsubscribed
FROM (SELECT A.name AS activity_t, E.subject AS subject, E.name AS email, A.activity_date , count(*) [count]
FROM (SELECT A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date
FROM marketo_tg.lead_activities A, marketo_tg.lead_activity_types T
WHERE T.name IN ('Send Email', 'Email Delivered', 'Open Email', 'Click Email', 'Unsubscribe Email') AND A.activity_type_id=T.id 
GROUP BY A.lead_id, A.primary_attribute_value_id, T.name, A.activity_date) A, marketo_tg.emails E
WHERE A.primary_attribute_value_id = E.id AND E.name LIKE '%Bounceback%'
GROUP BY email, activity_t, subject, A.activity_date)b
GROUP BY email, subject, activity_date