Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhWCV-0002Us-IY for pgsql-general@arkaria.postgresql.org; Tue, 06 May 2014 03:37:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WhWCU-000310-Pz for pgsql-general@arkaria.postgresql.org; Tue, 06 May 2014 03:37:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhWCS-00030n-FA for pgsql-general@postgresql.org; Tue, 06 May 2014 03:37:48 +0000 Received: from mail-pa0-x22d.google.com ([2607:f8b0:400e:c03::22d]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhWCK-0008IC-Oq for pgsql-general@postgresql.org; Tue, 06 May 2014 03:37:48 +0000 Received: by mail-pa0-f45.google.com with SMTP id ey11so5326551pad.4 for ; Mon, 05 May 2014 20:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-type:content-transfer-encoding:thread-index :content-language; bh=ePk2sT6AgY2uHvXAkpksFHnmSsmHBBr5d8GVdAV+Z28=; b=AgLn3F/pbcc3gx5g5+BELwW0Rrrzi44qvwYLI9ATiTeco7XxmRIN3TuCQdIJLFm9kH 4rB/fEpU2apdKuA+Tle+l1cgvjPQpUV5sxjFIDShXAtOOhn8cdtEDk6qq0m5et4BlJoZ vCzImNwmAqUa3pu7XB6WUIp2/zkAraxOPp54R80kBrlRuwuCYiAEGiAQttWyuYHaX1i2 PoohK0ekU4vPMb80DaZ42eyqKgMoz23q4xDg3kxjxHoaPMdBdKVW79ecvHNL75WpmFHC 4NzHPsbylwYj7hqCK4fLLiUX+Tb/VtXO5z4G1HkXUrVDh5O/TcTra5NlxosrxqC+tKkN 9izQ== X-Received: by 10.66.147.99 with SMTP id tj3mr1562746pab.47.1399347458717; Mon, 05 May 2014 20:37:38 -0700 (PDT) Received: from hplPC ([36.83.110.172]) by mx.google.com with ESMTPSA id lr3sm86167351pab.4.2014.05.05.20.37.35 for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 May 2014 20:37:37 -0700 (PDT) From: "Hengky Liwandouw" To: "'David G Johnston'" , References: <000601cf682e$2dce68c0$896b3a40$@com> <1399299903660-5802456.post@n5.nabble.com> <004201cf68d4$152dc3f0$3f894bd0$@com> <1399345258676-5802601.post@n5.nabble.com> In-Reply-To: <1399345258676-5802601.post@n5.nabble.com> Subject: Re: Crosstab function Date: Tue, 6 May 2014 11:37:29 +0800 Message-ID: <004601cf68dc$848e8420$8dab8c60$@com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac9o17a7KdFzsLrqRAG7JDQnkds5xgAAheGA Content-Language: en-us X-Pg-Spam-Score: -0.9 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org Very Clear instruction ! Thank you very much David. I will do it in my client app and follow your guidance. -----Original Message----- From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of David G Johnston Sent: Tuesday, May 06, 2014 11:01 AM To: pgsql-general@postgresql.org Subject: Re: [GENERAL] Crosstab function Hengky Lie wrote > 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 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: http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601. html Sent from the PostgreSQL - general mailing list archive at Nabble.com. -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general