From: Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
To: 'pgsql-sql@postgresql.org' <pgsql-sql@postgresql.org>
Subject: CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!
Date: Mon, 16 Aug 2004 09:46:41 +1000
Message-ID: <82E30406384FFB44AFD1012BAB230B55037D0504@shiva.au.lpint.net> (raw)
Does anyone know how to perform a cross-tab query in ONE SQL without having
to write a SP? The SQL at the end of this email attempts to display the
subquery result-set in a cross-tab format, it does not group the content
onto one row as it should in the sample below. SQL is below if it makes any
sense, however the sub-query returns data as below.
Examle:
Name Value
ID 1
Cola 10
Colb 20
Colc 30
Cold 40
Cole 50
I want to output as:
ID, cola, colb, colb, cold, cole
1 10 30 30 40 50
This is how it should output:
content_object_id | xpos | ypos | text | textangle | texttype
| symbol | linktype
-------------------+------+------+-------------------+-----------+----------
--+----------------+----------
100473 | 93 | 77 | text1 | 0 |
txt-pop3 | pop1_att | optional
Actual Output:
content_object_id | xpos | ypos | text | textangle | texttype
| symbol | linktype
-------------------+------+------+-------------------+-----------+----------
--+----------------+----------
100473 | 93 | | | |
| |
100473 | | 77 | | |
| |
|
100473 | | | text1 | |
| |
100473 | | | | 0 |
| |
100473 | | | | | txt-pop3
| |
100473 | | | | |
| pop1_att|
SQL:
select distinct mapInfo.content_object_id,
CASE WHEN mapInfo.node_id = 6957 THEN mapInfo.content END as xPos,
CASE WHEN mapInfo.node_id = 6958 THEN mapInfo.content END as yPos,
CASE WHEN mapInfo.node_id = 6959 THEN mapInfo.content END as text,
CASE WHEN mapInfo.node_id = 6960 THEN mapInfo.content END as
textAngle,
CASE WHEN mapInfo.node_id = 6961 THEN mapInfo.content END as
textType,
CASE WHEN mapInfo.node_id = 6962 THEN mapInfo.content END as symbol,
CASE WHEN mapInfo.node_id = 6963 THEN mapInfo.content END as
linkType
from ( SELECT child_node_names.node_id,
child_content_facts.content_object_id,child_node_names.node_name,
CASE WHEN child_node_names.node_id = 6962 THEN
(select node_name from node_names where
node_id = content_fact_versions.content)
WHEN child_node_names.node_id = 6961 THEN
(select node_name from node_names where
node_id = content_fact_versions.content)
WHEN child_node_names.node_id = 6963 THEN
(select node_name from node_names where
node_id = content_fact_versions.content)
ELSE
content_fact_versions.content
END
FROM content_objects_join
INNER JOIN content_objects as parent_content_objects
ON parent_content_objects.content_object_id =
content_objects_join.parent_cobj_id
INNER JOIN nodes as parent_nodes ON
parent_nodes.node_id = parent_content_objects.node_id
INNER JOIN node_names as
parent_node_names ON parent_node_names.node_id = parent_nodes.node_id
INNER JOIN content_facts as child_content_facts ON
child_content_facts.content_object_id = content_objects_join.child_cobj_id
INNER JOIN content_fact_versions ON
content_fact_versions.fact_id = child_content_facts.fact_id
INNER JOIN node_names as child_node_names ON
child_node_names.node_id = child_content_facts.node_id
WHERE parent_nodes.node_id = 341
--AND parent_content_objects.object_type_id in
(1,2,3,4,6,8,9)
AND parent_content_objects.object_type_id = 73
ORDER BY child_content_facts.content_object_id
) as mapInfo
group by mapInfo.content_object_id, xPos, yPos, text, textAngle, textType,
symbol, linkType
______________________________________________________________________
This email, including attachments, is intended only for the addressee
and may be confidential, privileged and subject to copyright. If you
have received this email in error, please advise the sender and delete
it. If you are not the intended recipient of this email, you must not
use, copy or disclose its content to anyone. You must not copy or
communicate to others content that is confidential or subject to
copyright, unless you have the consent of the content owner.
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: Theo.Galanakis@lonelyplanet.com.au
Subject: Re: CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!
In-Reply-To: <82E30406384FFB44AFD1012BAB230B55037D0504@shiva.au.lpint.net>
References: <82E30406384FFB44AFD1012BAB230B55037D0504@shiva.au.lpint.net>
* 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