CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!

From: Theo Galanakis <Theo(dot)Galanakis(at)lonelyplanet(dot)com(dot)au>
To: "'pgsql-sql(at)postgresql(dot)org'" <pgsql-sql(at)postgresql(dot)org>
Subject: CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!
Date: 2004-08-15 23:46:41
Message-ID: 82E30406384FFB44AFD1012BAB230B55037D0504@shiva.au.lpint.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Seltenreich 2004-08-16 01:03:32 Re: COMMENT ON CONSTRAINT
Previous Message Tom Lane 2004-08-15 19:43:18 Re: Getting points from polygon