Skip to content
Advertisement

Create an array in Snowflake

I want to create an array constructed from two timestamps. After that, use this as a range to find the difference between the first element and the second element.

The goal is to find the time difference between timestamps within business hours (9 am to 17 pm).

What should be the right approach here? Can I create arrays and then find the time difference?

e.g. in the first record, since started_at is after 17pm, the first element of array must be "2021-05-19 09:00:00".

Example data:

Expected result: enter image description here

Advertisement

Answer

The idea is to calculate if we need to extend range, generate rows using lateral flatten and calculate timestamps using case statements.

Demo:

Result:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement