Re: Use function to manipulate rows — how to get separate columns, rather than single row value

From: Guyren Howe <guyren(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Use function to manipulate rows — how to get separate columns, rather than single row value
Date: 2017-04-23 18:42:27
Message-ID: 6857DDAA-9B83-4BF2-9E01-5CDADCC7624A@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On Apr 23, 2017, at 11:34 , Guyren Howe <guyren(at)gmail(dot)com> wrote:
>
> I’m trying to write a function that manipulates whole rows. It returns the same type as the table it is being applied to, but when I select the function on the rows, I get a single column of row type, rather than separate columns.
>
> My function looks like:
>
> CREATE OR REPLACE FUNCTION reporting.formatted_cust_by_state_bold(c reporting_helpers.customers_by_state_ranked)
> RETURNS reporting_helpers.customers_by_state_ranked
> LANGUAGE plv8
> STABLE STRICT COST 1
> AS $function$
> if (c.rank == 1) {
> c['_meta'] = c['meta'] || {}
> c['_meta']['raw'] = c['_meta']['raw'] || {}
> c['_meta']['raw']['spent'] = c['_meta']['raw']['spent'] || {}
> c['_meta']['raw']['spent'] = "<b>" + c.spent + "</b>"
> }
> return c
> $function$
>
> I do this:
>
> SELECT formatted_cust_by_state_bold(c) FROM customers_by_state_ranked c
>
> How do I get the results as a full table?

And, of course, one always solves these things as soon as we ask for help.

SELECT (formatted_cust_by_state_bold(c)).*
FROM reporting_helpers.customers_by_state_ranked c

is the answer.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Clodoaldo Neto 2017-04-23 19:07:32 Failed dependencies for Pgadmin4 Web in Centos 7
Previous Message Guyren Howe 2017-04-23 18:34:51 Use function to manipulate rows — how to get separate columns, rather than single row value