Why this runtime error?
ERROR: column reference "arrive" is ambiguous
LINE 6: case when ( (cast('05:00' as time) >= arrive)
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
The thing is, the case statement is in a query, and I'm selecting from a table that does have a column called "arrive". I have not declared a variable called arrive. Why can't PG simply check to see that there's no such variable declared, and conclude that the reference must be to the column?
I am attached the code below. The only ostensible conflict I see is in the definition of the outgoing table returned by this function, in which there's a column called "arrive", and the use of the column name in the final select against the temporary table TT.
CREATE or replace function GetHourlyView
(v_whichDate date)
returns TABLE
(
stagecoach,
arrive time,
depart time,
t5am int,t6am int,t7am int,t8am int,t9am int,t10am int, t11am int,
t12pm int, t1pm int, t2pm int t3pm int,t4pm int,t5pm int,t6pm int,
t7pm int, t8pm int, t9pm int, t10pm int, t11pm int
)
as $body$
declare v_dow int := date_part('dow',v_whichDate);
begin
drop table if exists TT;
create temp table TT
(stagecoach varchar(25),
arrive time,
depart time,
t5am int,t6am int,t7am int,t8am int,t9am int,t10am int, t11am int,
t12pm int, t1pm int, t2pm int t3pm int,t4pm int,t5pm int,t6pm int,
t7pm int, t8pm int, t9pm int, t10pm int, t11pm int
) without OIDS on commit drop;
insert into TT
select * from
GetDailySchedule( v_whichDate);
-- (arrive=depart) means 'cancelled'
delete from TT where TT.arrive=TT.depart;
return QUERY
select
TT.stagecoach,
arrive,
depart,
case when ( (cast('05:00' as time) >= arrive) and (cast('05:00' as time) < depart )) then 1 else 0 end as t5am,
case when ( (cast('06:00' as time) >= arrive) and (cast('06:00' as time) < depart )) then 1 else 0 end as t6am,
<snip>
.
.
.
case when ( (cast('23:00' as time) >= arrive) and (cast('23:00' as time) < depart )) then 1 else 0 end as t11pm
from TT
;
drop table TT;
end
$body$
LANGUAGE 'plpgsql'