I'm using PostgreSQL for this. I need to count the calls to the sales department during the week and for those that came in between business hours. Here's my query so far:
SELECT
SUM(CASE WHEN call_logs.to_phone_number IN ('15125551234') THEN 1 ELSE 0 END) as sales_all,
SUM(
CASE WHEN
call_logs.to_phone_number IN ('15125551234') AND call_logs.start_time >= '08/08/2022 7:30 AM' AND call_logs.start_time <= '08/08/2022 5:30 PM'
OR
call_logs.to_phone_number IN ('15125551234') AND call_logs.start_time >= '08/09/2022 7:30 AM' AND call_logs.start_time <= '08/09/2022 5:30 PM'
OR
call_logs.to_phone_number IN ('15125551234') AND call_logs.start_time >= '08/10/2022 7:30 AM' AND call_logs.start_time <= '08/10/2022 5:30 PM'
OR
call_logs.to_phone_number IN ('15125551234') AND call_logs.start_time >= '08/11/2022 7:30 AM' AND call_logs.start_time <= '08/11/2022 5:30 PM'
OR
call_logs.to_phone_number IN ('15125551234') AND call_logs.start_time >= '08/12/2022 7:30 AM' AND call_logs.start_time <= '08/12/2022 5:30 PM'
THEN 1 ELSE 0 END) as sales_between_business_hours,
FROM call_logs
WHERE call_logs.start_time >= '08-08-2022 12:00 AM' AND call_logs.start_time <= '08-14-2022 11:59 PM';
This works, but the the second case statement to get sales_between_business_hours
seems a bit excessive. Anybody see a more elegant way to do this?