select
CASE WHEN (to_char(((case when v.trip_order_start_date is null then case when v.manual_start_date is null then t.required_pickup_date else v.trip_order_start_date end else v.trip_order_start_date end + (interval '-1 hours' * ofc.offset)) at time zone 'utc' at time zone 'America/Chicago' + (interval '-1 hours' * ofc.offset)) at time zone 'utc' at time zone 'America/Chicago', 'YYYYMM') < to_char((now() at time zone 'utc' at time zone 'America/Chicago'), 'YYYYMM')
AND(coalesce(posted_date, paid_date) IS NULL
OR to_char((coalesce(v.posted_date, v.paid_date) + (interval '-1 hours' * ofc.offset)) at time zone 'utc' at time zone 'America/Chicago', 'YYYYMM') > to_char((now() at time zone 'utc' at time zone 'America/Chicago' - interval '1' month), 'YYYYMM'))) THEN 'YES' ELSE 'NO' END AS accrual
WHERE
accrual = 'YES'
The problem is there is no table for me to reference the "accrual" column so I get the below error
[Code: 0, SQL State: 42703] ERROR: column "accrual" does not exist
Position: 10708 [Script position: 10708 – 10715]
Best Answer
Use the WITH clause to get the result set and SELECT only the wanted rows.
Or use the SELECT query as SUBSECT in a FROM clause and use that
In both cases you need a valid SELECT