pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: psql \set variables in crosstab queries?
Date: Sat, 4 Mar 2023 23:05:52 -0600
Message-ID: <50b9a782-09a3-e109-b687-90ad96755274@gmail.com> (raw)
In-Reply-To: <CAKFQuwbkNujc4-9RxSgUsUmhNnq1jfo3hwTTj9G7Osyc6=KjaQ@mail.gmail.com>
References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com>
	<CAKFQuwbkNujc4-9RxSgUsUmhNnq1jfo3hwTTj9G7Osyc6=KjaQ@mail.gmail.com>

On 3/4/23 19:32, David G. Johnston wrote:
> On Sat, Mar 4, 2023 at 5:20 PM Ron <ronljohnsonjr@gmail.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.
>

Ugh.  It's a long and hairy query that would be a nightmare in a format 
statement.

-- 
Born in Arizona, moved to Babylonia.

view thread (6+ messages)  latest in thread

Message-ID: <50b9a782-09a3-e109-b687-90ad96755274@gmail.com>
Permalink:  ../50b9a782-09a3-e109-b687-90ad96755274@gmail.com/
Also on:    postgresql.org/message-id/50b9a782-09a3-e109-b687-90ad96755274@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com, david.g.johnston@gmail.com
  Subject: Re: psql \set variables in crosstab queries?
  In-Reply-To: <50b9a782-09a3-e109-b687-90ad96755274@gmail.com>
  References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com> <CAKFQuwbkNujc4-9RxSgUsUmhNnq1jfo3hwTTj9G7Osyc6=KjaQ@mail.gmail.com> <50b9a782-09a3-e109-b687-90ad96755274@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox