pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sim Zacks <sim@compulab.co.il>
To: pgsql-general@postgresql.org
Subject: Re: Crosstab function
Date: Wed, 07 May 2014 14:32:50 +0300
Message-ID: <536A19E2.70607@compulab.co.il> (raw)
In-Reply-To: <004601cf68dc$848e8420$8dab8c60$@com>
References: <000601cf682e$2dce68c0$896b3a40$@com>
	<1399299903660-5802456.post@n5.nabble.com>
	<004201cf68d4$152dc3f0$3f894bd0$@com>
	<1399345258676-5802601.post@n5.nabble.com>
	<004601cf68dc$848e8420$8dab8c60$@com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

<html style="direction: ltr;">
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
  </head>
  <body style="direction: ltr;"
    bidimailui-detected-decoding-type="latin-charset" bgcolor="#FFFFFF"
    text="#000000">
    <div class="moz-cite-prefix">What I have done in the past to build a
      generic reporting application is to have the function write the
      results you want in a table and return the tablename and then have
      the client code call select * from that table. <br>
      <br>
      My standard report tablename is tblreport || userid;<br>
      It gets dropped at the beginning of the function, so it is
      basically a temp table that doesn't interfere with any other
      users.<br>
      <br>
      Example:<br>
      &nbsp;&nbsp;&nbsp; execute 'drop table if exists reports.tblreport' || v_userid ;<br>
      &nbsp;&nbsp;&nbsp; execute 'drop sequence if exists reports.tblreport' ||
      v_userid || '_id_seq; create sequence reports.tblreport' ||
      v_userid || '_id_seq';<br>
      &nbsp;&nbsp;&nbsp; v_sql=' create table reports.tblreport' || v_userid || ' as ';<br>
      <br>
      Sim<br>
      <br>
      On 05/06/2014 06:37 AM, Hengky Liwandouw wrote:<br>
    </div>
    <blockquote cite="mid:004601cf68dc$848e8420$8dab8c60$@com"
      type="cite">
      <pre wrap="">Very Clear instruction !

Thank you very much David. I will do it in my client app and follow your
guidance.


-----Original Message-----
From: <a class="moz-txt-link-abbreviated" href="mailto:pgsql-general-owner@postgresql.org">pgsql-general-owner@postgresql.org</a>
[<a class="moz-txt-link-freetext" href="mailto:pgsql-general-owner@postgresql.org">mailto:pgsql-general-owner@postgresql.org</a>] On Behalf Of David G Johnston
Sent: Tuesday, May 06, 2014 11:01 AM
To: <a class="moz-txt-link-abbreviated" href="mailto:pgsql-general@postgresql.org">pgsql-general@postgresql.org</a>
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
</pre>
      <blockquote type="cite">
        <pre wrap="">Hi David,

Are you sure that there is no pure sql solution for this ? 

I think (with my very limited postgres knowledge), function can solve
this.

Which is the column header I need but I really have no idea how to use
this
as column header.

Anyway, If i can't do this in postgres, I will try to build sql string in
the client application (Windev) and send the fixed sql to the server
</pre>
      </blockquote>
      <pre wrap="">
Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various "quote_" functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context:
<a class="moz-txt-link-freetext" href="http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601">http://postgresq...;.
html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


</pre>
    </blockquote>
    <br>
  </body>
</html>




view thread (23+ messages)  latest in thread

Message-ID: <536A19E2.70607@compulab.co.il>
Permalink:  ../536A19E2.70607@compulab.co.il/
Also on:    postgresql.org/message-id/536A19E2.70607@compulab.co.il

 · 

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: sim@compulab.co.il
  Subject: Re: Crosstab function
  In-Reply-To: <536A19E2.70607@compulab.co.il>
  References: <000601cf682e$2dce68c0$896b3a40$@com> <1399299903660-5802456.post@n5.nabble.com> <004201cf68d4$152dc3f0$3f894bd0$@com> <1399345258676-5802601.post@n5.nabble.com> <004601cf68dc$848e8420$8dab8c60$@com> <536A19E2.70607@compulab.co.il>

* 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