row_to_json on a subset of columns. - Mailing list pgsql-general

From Chris Hanks
Subject row_to_json on a subset of columns.
Date
Msg-id CAK7KUdB64hTsWqBWy9R-g+mebajSra3Uz73zQuMxCyQLYLHUQA@mail.gmail.com
Whole thread Raw
Responses Re: row_to_json on a subset of columns.
List pgsql-general
I'm using a JSON column to store some aggregate data, like so:

    UPDATE courses_table
    SET aggregates = agg.aggregates
    FROM (
      SELECT course_id, row_to_json(sub) AS aggregates
      FROM (
        SELECT course_id, avg(rating) AS rating, sum(reviews_count) AS
reviews_count,
          sum(user_started_count)   AS user_started_count,
sum(all_user_started_count)   AS all_user_started_count,
          sum(user_completed_count) AS user_completed_count,
sum(all_user_completed_count) AS all_user_completed_count
        FROM course_details_table
        GROUP BY course_id
      ) sub
    ) agg
    WHERE courses_table.id = agg.course_id;

This works, but also stores the course_id in the JSON document. Is
there a relatively clean way to remove it? The suggestions I got in
#postgresql on freenode were to remove the course_id from the
innermost select, but that would break the outer queries, or to use
row() to select only a few of the columns, which loses their column
names. I'm on PG 9.3.3.

Thanks!
Chris


pgsql-general by date:

Previous
From: Dmitry Samonenko
Date:
Subject: Re: Fwd: libpq: indefinite block on poll during network problems
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Fwd: libpq: indefinite block on poll during network problems