简体   繁体   English

选择每个联系人的最早日期

[英]Select earliest date for each Contact

I have the following query, it should pull just the earliest date for each contact, but it's pulling all available dates. 我有以下查询,它应该只为每个联系人拉最早的日期,但它拉所有可用的日期。 I have reviewed multiple threads here but wasn't able to solve it. 我在这里检查了多个线程,但无法解决。 This is using SQL Server 2005. 这正在使用SQL Server 2005。

SELECT DISTINCT o.SubscriberKey, MIN(o.EventDate) as OpenDate
FROM _Open o 
INNER JOIN _Job j
    ON o.JobID = j.JobID
GROUP BY o.SubscriberKey, o.EventDate

Currently, I am getting results like this: 目前,我得到这样的结果:

Subscriber 1  17 July 2019 06:04
Subscriber 1  17 July 2019 06:05
Subscriber 1  18 July 2019 04:29
Subscriber 2  18 July 2019 07:04
Subscriber 2  18 July 2019 07:21
Subscriber 2  24 July 2019 05:40

And what I would like to achieve: 我想实现的目标是:

Subscriber 1  17 July 2019 06:04
Subscriber 2  18 July 2019 07:04
SELECT 
     --you dont want to have a DISTINCT here, you are doing a GROUP BY so its not needed 
    --DISTINCT
    o.SubscriberKey
    , MIN(o.EventDate) as OpenDate
FROM _Open o 
    --Join is not referenced. Thanks @Gordon Linoff
    --INNER JOIN _Job j
    --    ON o.JobID = j.JobID
GROUP BY
    o.SubscriberKey
    --dont group on Event date, this is stopping the MIN function from aggregating the rows.
    --, o.EventDate

You don't need a JOIN for this. 您不需要JOIN And you need to fix the GROUP BY : 您需要修复GROUP BY

SELECT o.SubscriberKey, MIN(o.EventDate) as OpenDate
FROM _Open o 
GROUP BY o.SubscriberKey;

You only need the JOIN if it is filtering results, but I doubt that is the case. 如果要过滤结果,则只需要JOIN ,但我怀疑情况确实如此。

You only need to group by SubscriberKey and aggregate on EventDate . 您只需EventDate group by SubscriberKey并在EventDate聚合。
Also you are joining _Job but you don't use it. 另外,您正在加入_Job但是您不使用它。
Unless you only want to get the minimum EventDate of the matching rows of the tables there is no need for this join: 除非您只想获取表中匹配行的最小EventDate ,否则不需要此联接:

SELECT SubscriberKey, MIN(EventDate) as OpenDate
FROM _Open 
GROUP BY SubscriberKey

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM