1

I have a requirement wherein I have to query a database for the following condition.

Database [Report Pull] has let us say 2 columns
CustomerID ReportDt

I have have to find all the customers who have not have a record in last 30 days but have a record exactly (today - 30 ) among other conditions.

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
)

Now i want to pull customers such that

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

and so on for every 30 days. This is because the db can have multiple records of report pull. I know it is a bit confusing, but please help me. :)

Please note we cannot declare any variable in SQL. The DB is Salesforce Marketing CLoud aka ExactTarget

1
  • Sorry, I've just read over your question again. Could you share what you want the final output of this query should to look like? "And so on" for how long?
    – Macca
    Feb 2, 2017 at 12:09

1 Answer 1

0

So you want to find all customers whose records were pulled on every 30*x days? In perpetuity? So 30 days 60 days 90 days and son on?

SELECT 
CustomerID, 
ReportDt
FROM [Report Pull]
WHERE MOD(DATEDIFF(DAY, DATEADD (DAY, 0, CURRENT_TIMESTAMP), ReportDt),30) = 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .