Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 8BA849FB48A for ; Fri, 29 Jun 2007 01:57:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 72798-01 for ; Fri, 29 Jun 2007 01:57:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by postgresql.org (Postfix) with ESMTP id 8C0F39FB46A for ; Fri, 29 Jun 2007 01:57:39 -0300 (ADT) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu0) with ESMTP (Nemesis), id 0MKwh2-1I48Y60bJ7-0004LR; Fri, 29 Jun 2007 06:57:38 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1I48Y5-0004hp-00 for pgsql-sql@postgresql.org; Fri, 29 Jun 2007 06:57:37 +0200 Date: Fri, 29 Jun 2007 06:57:37 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: record fields as columns in reports Message-ID: <20070629045737.GA17977@a-kretschmer.de> References: <20070628144615.GQ72343@spoetnik.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070628144615.GQ72343@spoetnik.xs4all.nl> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX18ZLSV1+/xEjdrXxbaR2410AdXcYj4SSwGaG+6 qI/9WE4tkUThfigHvldebQGE/jheiW1ikEP8Y405SKXphGPVPh NjTL7cInitOHwz0eKxen9dGmBTRWO/O X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/242 X-Sequence-Number: 28732 am Thu, dem 28.06.2007, um 16:46:15 +0200 mailte Reinoud van Leeuwen folgendes: > Hi everybody, > > I have a script that runs every night and produces a list of a few error > conditions and the number. > > My manager would like it in a form where he can produce some charts in a > spreadsheet (that's probably why he's a manager ";-). > > So the table I store the errormessages in has a format like this: > > CREATE TABLE repport_history > ( > rundate date, > errordescription character varying(255), > number bigint > ) > > > And I would like an output with something like: > > 06/22 06/23 06/24 06/25 > ERROR1 10 10 9 8 > ERROR2 250 300 220 200 > ERROR3 4 2 0 0 > > > (probably in csv or something like that but that is the easy part ";-) > > The problems are: > - how to create a dynamic result type? (is that possible in a stored > procedure?) > - how to make sure that missing records are reported as 0 > (some errors might not have entries on some dates) You can do it with conditionals. Circa: select errordescription, sum (case when rundate = '2007-06-22'::date then number else 0 end) as "06/22", sum(case when rundate = '2007-06-23'::date then number else 0 end) as "06/23" ... group by errordescription order by errordescription; and you can use a VIEW and change the fix conditions to expressions with current_date. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net