select case when gd_deacttime is null then 1 else 0 end as deact, gd_groupdataid, gd_finname as name from groupdata where gd_deacttime is not NULL OR gd_groupdataid in (select final_groupid from finalproduct where final_deacttime is not null) or gd_groupdataid in (select raw_groupid from rawproduct where raw_deacttime is not null) order by upper(name); ERROR: column "name" does not exist