News for hackers. Stuff that matters.
Re:My Favorite PostgreSQL Queries and Why They Matter by ilikepi on Sunday April 15, 2018 @01:25AM (#16841375)
Ditto, thanks for sharing the articles.

For a trivially small set of mapping data, I like using VALUES to create a "virtual" staging table:

    UPDATE reward_members
    SET member_status = status_map.new_status
    FROM (VALUES ('gold',     'gold_group'),
                 ('bronze',   'bronze_group'),
                 ('platinum', 'platinum_group'),
                 ('silver',   'silver_group')
         ) AS status_map (old_status, new_status)
    WHERE member_status = status_map.old_status;

I find this to be more readable than the CASE statement version.
Re:My Favorite PostgreSQL Queries and Why They Matter by infogulch on Sunday April 15, 2018 @04:24PM (#16844751)
I was looking through the thread for this solution. It's great because it's much harder to mess up and is less verbose than a case, and it's built on common SQL features making it portable and fast and easy to grok (as long as both the engine and the dba knows how joins work).

As a data analyst this technique is also great for scrubbing dirty data at query time when you don't have the ability to just fix it.

Re:My Favorite PostgreSQL Queries and Why They Matter by ilikepi on Sunday April 15, 2018 @04:32PM (#16844786)
Yeah, I use it in queries as well. Good point!