简体   繁体   中英

How to join a SQL (Teradata) query with time condition?

I am trying to capture every single email including today until the next day morning at 10.30 AM. I know I to have add logic on my join and I am not sure I am doing it correctly. Can you anybody please tell is this correct or is there any better way to do it?

what I try to add to the JOIN

LEFT JOIN events  b ON a.signup_dt BETWEEN b.mst_dt  AND  b.mst_dt +1 AND Extract(HOUR From  b.mst_dt+1) <= 9 
AND a.email_address = b.email_address

Date from event (I have email address column that I did not include here) enter image description here

Date from Ops table ( I have datetime colunm called signup_dttm) enter image description here

Clarification - On my ops table I have all the emailed captures for the particular date. We will send a info email for every email we captured from Ops table. That data (Which is email we send in Ops table and date and time) stored in event table. For example we captured 500 emails yesterday(12/11/2019). Then we have send 400 info emails yesterday. Other 100 emails that left we will send it today (12/12/19) sometime before 10.30 AM . Again those send data will store in event table. That is why I am trying capture everything from yesterday to today 9.00 AM and join to the ops table. Hopefully this will make sense if not please let me know. Any help will highly appreciated.

Thank you so much

You can use an INTERVAL for your join condition:

a.signup_dt BETWEEN 
  CAST(b.mst_dt AS TIMESTAMP(0)) AND -- Current day
  (CAST(b.mst_dt AS TIMESTAMP(0)) + INTERVAL '1 09' DAY TO HOUR) -- 9am next day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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