0

Relatively new SQL user question....

If my postgresql query looks like this:

   select
        to_timestamp((unnest(enrolled_ranges) ->> 'start_time')::float) as start_time
        , to_timestamp((unnest(enrolled_ranges) ->> 'end_time')::float) as end_time
    from student_inclusions
 where student_id = '123456'

And the initial enrolled_ranges json data is this:

{"{\"start_time\":1536652800.00007,\"end_time\":1563981839.966626}","{\"start_time\":1563982078.624668,\"end_time\":1563989693.830777}"}

Why does sql do this

enter image description here

instead of this

enter image description here

The first answer is what I want, I just don't understand how sql knows from the query to associate the matching start and end times. Do you have any insight?

cyoung
  • 23
  • 4

1 Answers1

1

The documentation on set-returning functions describes the behavior you observed:

For each row from the underlying query, there is an output row using the first result from each set-returning function, then an output row using the second result, and so on.

See also What is the expected behaviour for multiple set-returning functions in SELECT clause?

SebDieBln
  • 3,303
  • 1
  • 7
  • 21