获取正确输出所需的SQL输入

时间:2012-06-27 16:43:23

标签: sql null

Select A.SubscriberKey, A.EventDate,B.CreatedDate
From _Click A
JOIN _ListSubscribers B
ON A.SubscriberKey = B.SubscriberKey
Where B.ListID = '10630' AND B.CreatedDate > (Select DATEADD(day,-180,getdate())) AND A.EventDate IS NULL
Group By A.SubscriberKey,B.CreatedDate, A.EventDate

目前,没有任何东西被归还。我想返回Subscribers SubscriberKey(这是他们的电子邮件),EventDate(点击发生的日期),以及他们没有点击任何内容时添加的日期(CreatedDate)。谁能帮助我指出正确的方向?谢谢大家!

2 个答案:

答案 0 :(得分:1)

尝试:

SELECT A.SubscriberKey, A.EventDate, B.CreatedDate 
FROM _Click A, _ListSubscribers B 
WHERE A.SubscriberKey(+) = B.SubscriberKey 
AND B.ListID = '10630' 
AND B.CreatedDate > (Select DATEADD(day,-180,getdate())) 
AND nvl(A.EventDate,null) IS NULL 
GROUP BY A.SubscriberKey,B.CreatedDate, A.EventDate

答案 1 :(得分:0)

在这种情况下,我没有看到GROUP BY的目的。你没有做任何聚合。

我相信这会给你想要的结果。

select SubscriberKey,
       null as EventDate,
       CreatedDate
  from _ListSubscribers ls
 where ListID='10630'
   and CreatedDate > DateAdd( day, -180, getdate())
   and not exists( select 1 from _Click c where c.SubscriberKey = B.SubscriberKey )

以下使用外部联接的查询应该给出相同的结果,并且它与原始查询更相似。

select ls.SubscriberKey,
       c.EventDate,
       ls.CreatedDate
  from _ListSubscribers ls
  left outer join _Click c
    on c.SubscriberKey = ls.SubscriberKey
 where ListID='10630'
   and CreatedDate > DateAdd( day, -180, getdate())
   and c.EventDate is null