SELECT * FROM
(select
flight_id,
flight_no,
scheduled_departure,
scheduled_arrival,
cast (scheduled_departure as text) TIMINGS
from bookings.flights), as TABLE1
case when TIMINGS
between 02:00:00+05:30 to 06:00:00+05:30 then 'Early morning flights'
when scheduled_departure to scheduled_arrival
between 06:00:00+05:30 to 11:00:00+05:30 then 'Morning flights'
when scheduled_departure to scheduled_arrival
between 11:00:00+05:30 to 16:00:00+05:30 then 'Noon flights'
when scheduled_departure to scheduled_arrival
between 16:00:00+05:30 to 19:00:00+05:30 then 'Evening flights'
when scheduled_departure to scheduled_arrival
between 19:00:00+05:30 to 23:00:00+05:30 then 'Night flights'
when scheduled_departure to scheduled_arrival
between 23:00:00+05:30 to 02:00:00+05;30 then 'Night flights'
END as TIMINGS
This is the ERROR
ERROR: subquery in FROM must have an alias
LINE 1: SELECT * FROM (select
^
HINT: For example, FROM (SELECT ...) [AS] foo.
SQL state: 42601
Character: 15
You can get what you are looking for without CASE at all.
One thing to realize is when working with dates/timestamps converting to text is virtually always unnecessary and incorrect (as is the case here) basically just do not do it. Use the Postgres date functions.
The following gives what you are wanting. It begins creating a CTE to define time periods and a descriptive name for each period. It then JOINS that CTE with the flight table. (See demo here)
Notes:
::time(0). It does, however, round to the nearest second.23:00:00will always be greater than02:00:00. The change corrects for this.I am a newbie so bear with me if the format is not structured. so I got the expected output after writing this code...
''' but in the modified column I just want the Text such as 'Night Flight' instead of the entire time which is '00:00:00'::time, '02:00:00'::time, 'Night flight' how can I get just the text output?
2nd I tried to filter the output using
having'orwherestatement using this codeit is giving me an error saying this
ERROR: input of anonymous composite types is not implemented LINE 22: having flights = 'Morning flight'
(Pasting text because image is allowed in my account)
@belayer
Response:
flights = 'Morning flight,flightsrefers to the entire set on rows within the CTE. The usage would be the same as if there were a tableflights, you would not saytable_name = string. In this case you are looking forflights.description = 'Morning flight. You will get a similar error on the line aliased as "Timings". You will need to refer to the individual columns.