【问题标题】:Getting data from Database using n number of times使用 n 次从数据库中获取数据
【发布时间】:2017-02-02 01:10:13
【问题描述】:

我有一个要求,我必须在数据库中查询以下条件。

数据库 [Report Pull] 让我们说 2 列
客户 ID ReportDt

我必须找到过去 30 天内没有记录但准确记录(今天 - 30 天)的所有客户。

Select Condition on [Report Pull] PR
and cast(PR.ReportDt as Date) = cast(getdate()-30 as date)
and not exists (
                select   PR2.*
                from [Report Pull] PR2
                where 1=1
                and cast(PR2.ReportDt as Date) > cast(getdate()-30 as date)
                and PR2.CustomerId = PR.CustomerId
)

现在我想吸引这样的客户

cast(getdate()-30 as date) is mod30 = 0
AND at the same time
cast(PR2.ReportDt as Date) > cast(getdate()-30 as date)

Next 
cast(getdate()-60 as date) is mod30 = 0
AND at the same time
cast(PR2.ReportDt as Date) > cast(getdate()-60 as date)
that is no report pulled in last 60 days

以此类推,每 30 天一次。这是因为数据库可以有多个报告拉取记录。 我知道这有点令人困惑,但请帮助我。 :)

请注意,我们不能在 SQL 中声明任何变量。数据库是 Salesforce Marketing Cloud 又名 ExactTarget

【问题讨论】:

    标签: sql exacttarget


    【解决方案1】:

    我不是 100% 清楚您要做什么,但我认为您可以通过表的单次传递(没有自联接)来完成此操作。请注意,我并不是说自联接不好。

    考虑一下:

    select
      CustomerID,
      max (case when cast(ReportDt as Date)=cast(getdate()-30 as date) then 1 else 0 end) EQ30,
      max (case when cast(ReportDt as Date)>cast(getdate()-30 as date) then 1 else 0 end) GT30,
      max (case when cast(ReportDt as Date)=cast(getdate()-60 as date) then 1 else 0 end) EQ60,
      max (case when cast(ReportDt as Date)>cast(getdate()-60 as date) then 1 else 0 end) GT60
    from
      [Report Pull]
    group by
      CustomerID
    

    它应该产生一个数据集:

    Customer ID      30 Days ago?   Last 30 Days    60 Days ago?    Last 60 days
    -----------      ------------   ------------    ------------    ------------
    

    其中 1 = 真,0 = 假。从这里开始,我认为将其扩展到其他时间范围并逐个客户评估他们所在的类别会很容易。

    同样,我不确定您想要的最终输出是什么,但希望这是一个可以借鉴的概念。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 2012-03-04
      • 2016-11-21
      • 2014-04-10
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多