查找汇总SQL

时间:2018-10-12 19:21:27

标签: sql teradata

我在此表中包含字段(campaign_launch_date,email_address_id)。电子邮件被发送给用户以诱使他们购买。我想弄清楚在campaign_launch_date之后的7天内是否有任何email_addresses购买了任何东西(以及订单总额)。

我拥有的表:
表1:(campaign_launch_date,email_address_id,launch_id)
表2:(email_address_id,user_id)
表3:(user_id,sales_transaction_date,order_amount)

这是我到目前为止所拥有的:

SELECT    t1.launch_id, 
          t3.user_id, 
          Sum(order_amount), 
          Count(order_amount)
FROM      t1 
JOIN      t2 
ON        t1.email_address_id = t2.email_address_id 
LEFT JOIN 
ON        t3.user_id = t2.user_id 
AND       t1.campaign_launch_date <= (t3.sales_transaction_date + interval '7' day) 
GROUP BY  1, 
          2

3 个答案:

答案 0 :(得分:1)

SELECT    t1.launch_id, 
          t3.user_id, 
          Sum(order_amount), 
          Count(order_amount)
FROM      t1 
JOIN      t2 
ON        t1.email_address_id = t2.email_address_id 
LEFT JOIN t3
ON        t2.user_id = t3.user_id 
WHERE     t1.campaign_launch_date <= (t3.sales_transaction_date + interval '7' day) 
GROUP BY  1, 
          2

答案 1 :(得分:1)

我认为您的日期逻辑不正确。如果我理解正确:

select t1.launch_id, t3.user_id, sum(order_amount), count(order_amount)
from t1 join
     t2
     on t1.email_address_id = t2.email_address_id left join
     t3
     on t3.user_id = t2.user_id and
        t3.sales_transaction_date >= t1.campaign_launch_date  and
        t3.sales_transaction_date <= t1.campaign_launch_date + INTERVAL '7' DAY)
group by 1, 2;

答案 2 :(得分:1)

像这样吗?

for element in tweets:
    for s in element.get('statuses'):
       place = s.get('place')
       print(place['name'])