I am looking to write a query specifying a time of 7am-3pm, 3pm-11pm, and 11pm-7am which "only" looks at the prior 24 hours. I currently have a query which looks at the previous 24 hour but I want the previous 24 hour broken out by the above time slots. Any help would be appreciated.
This is the current query which just takes into account the previous day....
SELECT "inmain"."calltime", "inmain"."business", "inmain"."agency", "inmain"."case_id", "inmain"."nature", "inmain"."street", "inmain"."inci_id"
FROM "cad"."dbo"."inmain" "inmain"
WHERE "inmain"."agency" = 'YUPD'
AND "inmain"."calltime" > getdate()-1
ORDER BY "inmain"."nature"
1条答案
按热度按时间tmb3ates1#
You could create a scalar-valued function that changes a datetime to an ID. There are a lot of ways to implement this. Here's one that turns a date into YYYYMMDDS, where S is the time slot (1 (7a to 3p) through 3 (11p to 7a)).
So, for example, 3/24/2023 6:00p would become 202303242.
Here's the function...
And here's how you use it...
You could then use that ID to compare to earlier IDs, etc. Or tweak the whole thing so that it returns the date and slot separately, being careful to drop back one day for shifts that start between Midnight and 7a.
For your specific requirement, you would change the where clause: