Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I like using HAVING just to have conditions that reference expressions from the SELECT columns.

e.g. rather than having to do

    SELECT
        COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) AS district,
        ...
    FROM ...
    WHERE COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) IS NOT NULL
just do

    SELECT
        COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) AS district,
        ...
    FROM ...
    HAVING district IS NOT NULL
Hopefully the optimizer understands that these are equivalent, I haven't checked.


This should work with WHERE too at least as long as the name given using AS in the SELECT is given to a row and not to an aggregate.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: