pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-sql@postgresql.org
Subject: Re: PIVOT of data
Date: Wed, 14 Mar 2001 21:04:29 -0800
Message-ID: <web-25229@davinci.ethosmedia.com> (raw)
In-Reply-To: <20010314212627.H12986@mail.perdue.net>

Srikanth,

	Aside from seconding Tim (your database is *not* normalized), the PIVOT
function is only available one of 2 ways:

SQL-ONLY, FIXED COLUMN:  If you know your categories in advance, you can
write a complex view using outer joins, sub-selects, and aggregates.
Imagine that we know your types of marketing to be limited to TV, NEWS
and POSTER:

SELECT client_address, tv_totals.totaltv, news_totals.totalnews,
poster_totals.totalposter
FROM clients
LEFT OUTER JOIN (
	SELECT client_address, sum(tv_spots) as totaltv
	FROM clients
	GROUP BY client_address ) AS tv_totals
	ON tv_totals.client_address = clients.client_address
LEFT OUTER JOIN (
	SELECT client_address, sum(news_ads) as totalnews
	FROM clients
	GROUP BY client_address ) AS news_totals
	ON news_totals.client_address = clients.client_address
LEFT OUTER JOIN (
	SELECT client_address, sum(posters) as totalposters
	FROM clients
	GROUP BY client_address ) AS poster_totals
	ON poster_totals.client_address = clients.client_address;


DYNAMIC METHOD:  This requires an external programming language, and I'm
not going to write it for you!  However, pick you favorite procedural
language (tcl, perl, or C) and write the following:

1. Do an select distinct to obtain each value in the column you want to
use in the headers.  Load this series into an array.

2. Dynamically generate query text, adding a column and a LEFT OUTER
JOIN as above into the query for each value in your array.

This method is a lot of work for both you and the server.  Why do you
think that MS Access often crashes on a large "crosstab" query?

-Josh Berkus


______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



view thread (4+ messages)

Message-ID: <web-25229@davinci.ethosmedia.com>
Permalink:  ../web-25229@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-25229@davinci.ethosmedia.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-sql@postgresql.org
  Cc: josh@agliodbs.com
  Subject: Re: PIVOT of data
  In-Reply-To: <web-25229@davinci.ethosmedia.com>
  References: <20010314212627.H12986@mail.perdue.net> <web-25229@davinci.ethosmedia.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