pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!
4+ messages / 2 participants
[nested] [flat]

* CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong!
@ 2004-08-15 23:46  Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
  0 siblings, 1 reply; 4+ messages in thread

From: Theo Galanakis @ 2004-08-15 23:46 UTC (permalink / raw)
  To: 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.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CROSS-TAB query help? I have read it cant be done in one
@ 2004-08-16 08:06  Richard Huxton <dev@archonet.com>
  parent: Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
  0 siblings, 0 replies; 4+ messages in thread

From: Richard Huxton @ 2004-08-16 08:06 UTC (permalink / raw)
  To: Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>; +Cc: pgsql-sql

Theo Galanakis wrote:
> 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

> Actual Output:
> 
>  content_object_id | xpos | ypos |       text        | textangle |  texttype
> |     symbol     | linktype
> -------------------+------+------+-------------------+-----------+----------
>             100473 | 93   |      |                   |           |
>             100473 |      | 77   |                   |           |
>             100473 |      |      | text1            |           |

Don't forget the provided crosstab functions (in contrib/). If you don't 
want that, you could aggregate your results:

SELECT content_object_id, MAX(xpos), MAX(ypos), ...
FROM (
<your query here>
) AS raw
GROUP BY content_object_id;

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CROSS-TAB query help? I have read it cant be done in on
@ 2004-08-17 02:47  Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
  0 siblings, 1 reply; 4+ messages in thread

From: Theo Galanakis @ 2004-08-17 02:47 UTC (permalink / raw)
  To: pgsql-sql


Thanks Rickard 
Max may not work as not all the data is numerical. However I will give the
contrib/cross-tab a go!

Theo
-----Original Message-----
From: Richard Huxton [mailto:dev@archonet.com] 
Sent: Monday, 16 August 2004 6:06 PM
To: Theo Galanakis
Cc: 'pgsql-sql@postgresql.org'
Subject: Re: [SQL] CROSS-TAB query help? I have read it cant be done in one
SQL, pro ve them wrong!


Theo Galanakis wrote:
> 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

> Actual Output:
> 
>  content_object_id | xpos | ypos |       text        | textangle |
texttype
> |     symbol     | linktype
> -------------------+------+------+-------------------+-----------+----
> -------------------+------+------+-------------------+-----------+----
> -------------------+------+------+-------------------+-----------+--
>             100473 | 93   |      |                   |           |
>             100473 |      | 77   |                   |           |
>             100473 |      |      | text1            |           |

Don't forget the provided crosstab functions (in contrib/). If you don't 
want that, you could aggregate your results:

SELECT content_object_id, MAX(xpos), MAX(ypos), ...
FROM (
<your query here>
) AS raw
GROUP BY content_object_id;

-- 
   Richard Huxton
   Archonet Ltd


______________________________________________________________________
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.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CROSS-TAB query help? I have read it cant be done in on
@ 2004-08-17 09:48  Richard Huxton <dev@archonet.com>
  parent: Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
  0 siblings, 0 replies; 4+ messages in thread

From: Richard Huxton @ 2004-08-17 09:48 UTC (permalink / raw)
  To: Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>; +Cc: pgsql-sql

Theo Galanakis wrote:
> Thanks Rickard 
> Max may not work as not all the data is numerical. However I will give the
> contrib/cross-tab a go!

It will work as long as you only have one non-null value per grouped 
field (which you did in your example).

>>|     symbol     | linktype
>>-------------------+------+------+-------------------+-----------+----
>>-------------------+------+------+-------------------+-----------+----
>>-------------------+------+------+-------------------+-----------+--
>>            100473 | 93   |      |                   |           |
>>            100473 |      | 77   |                   |           |
>>            100473 |      |      | text1            |           |


-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2004-08-17 09:48 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-08-15 23:46 CROSS-TAB query help? I have read it cant be done in one SQL, pro ve them wrong! Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
2004-08-16 08:06 ` Re: CROSS-TAB query help? I have read it cant be done in one Richard Huxton <dev@archonet.com>
2004-08-17 02:47 Re: CROSS-TAB query help? I have read it cant be done in on Theo Galanakis <Theo.Galanakis@lonelyplanet.com.au>
2004-08-17 09:48 ` Re: CROSS-TAB query help? I have read it cant be done in on Richard Huxton <dev@archonet.com>

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