Re: psql \set variables in crosstab queries?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Ron <ronljohnsonjr(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: psql \set variables in crosstab queries?
Date: 2023-03-05 01:32:21
Message-ID: CAKFQuwbkNujc4-9RxSgUsUmhNnq1jfo3hwTTj9G7Osyc6=KjaQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Mar 4, 2023 at 5:20 PM Ron <ronljohnsonjr(at)gmail(dot)com> wrote:

>
> But crosstab takes text strings as parameters. How then do you use \set
> variables in crosstab queries?
>
>
You need to dynamically write the textual query you want to send to the
crosstab function. In particular that means writing it using "format()"
and then substituting the values into the query via placeholders.

Roughly like:

SELECT * FROM crosstab(format('SELECT * FROM %I WHERE %I = %L, :'tbl_name',
:'col_name', :'compare_value'));

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ron 2023-03-05 05:03:37 Re: psql \set variables in crosstab queries?
Previous Message Tom Lane 2023-03-05 01:22:18 Re: psql \set variables in crosstab queries?