1

Hello all I am trying to get timestamps from today and yesterday. The thing is my query is only returning rows for today - am I missing something? I normally dont use multiple conditions in a JOIN statement but my logic seems like it makes sense

I am using dateadd to minus 1 day but its still returning only aug 8 entries - see screenshot as well

SELECT O.subscriber_key, O.timestamp, O.data from  abandoned_browse_1643376986  O
JOIN ENT._Subscribers s ON O.subscriber_key = s.SubscriberKey
AND  
(
cast(O.timestamp As Date) = cast(GetDate() As Date)     
OR 
cast(DATEADD(day,-1,O.timestamp )As Date) = 
cast(DATEADD(day,-1, GetDate())  as DATE)
)

enter image description here

2 Answers 2

1

Did you try something like the following, using the WHERE clause ?

SELECT O.subscriber_key, O.timestamp, O.data
FROM abandoned_browse_1643376986 O
JOIN ENT._Subscribers S ON O.subscriber_key = S.SubscriberKey
WHERE 
    DATEDIFF(DAY, O.timestamp, GETDATE()) = 0
OR
    DATEDIFF(DAY, O.timestamp, GETDATE()) = 1

Orig DE :

Orig DE

Filtered DE with only today's and yesterday's records :

Filtered DE

5
  • THanks - your query seems to look good. I actually wrote the below it returned more than double your query - both seem to have the same logic, have any idea why? SELECT DISTINCT O.subscriber_key, O.timestamp, O.data from abandoned_browse_1643376986 O JOIN ENT._Subscribers s ON O.subscriber_key = s.SubscriberKey WHERE cast(O.timestamp As Date) >= cast(DATEADD(day,-1,getdate() )As Date)
    – GS65
    Commented Aug 8, 2022 at 22:47
  • Do you get timestamps from today and yesterday now ? Commented Aug 9, 2022 at 7:06
  • Only today which is weird (for my query)
    – GS65
    Commented Aug 9, 2022 at 7:59
  • 3
    @GS65 : Made some enhancements and added a test result : you should be good to go with this one. Also, you may not actually need a join on All Subscribers, except maybe to insert only existing subscribers. Commented Aug 9, 2022 at 11:55
  • 1
    THanks for the help I appreciate it!
    – GS65
    Commented Aug 9, 2022 at 13:38
1

Here's how I generally select rows from yesterday:

SELECT 
  O.subscriber_key
, O.timestamp
, O.data 
from abandoned_browse_1643376986 O
JOIN ENT._Subscribers s ON O.subscriber_key = s.SubscriberKey
where 
o.timestamp >= convert(date, getDate()-1)
and o.timestamp < convert(date, getDate())

Convert is native to SQLServer and is allegedly faster. When converting to a date you're effectively resetting the time part of the timestamp to midnight. So the translation of my example above would be:

select rows after yesterday at midnight and include anything before midnight today

Using sargable where-clauses is a good thing performance-wise.

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