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

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

am 16.08.2004 01:46:41 von Theo.Galanakis

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C48322.1D7A5660
Content-Type: text/plain


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.
------_=_NextPart_001_01C48322.1D7A5660
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable






CROSS-TAB query help? I have read it cant be done in one SQL, prove =<br /> them wrong!



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 grou=
p the content onto one row as it should in the sample below. SQL is below i=
f it makes any sense, however the sub-query returns data as below.
P>

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  &nbs=
p;   30      30    &=
nbsp; 40      50



This is how it should output:



content_object_id | xpos | ypos |    =
   text        | textangle |&n=
bsp; texttype  |     symbol    =
; | linktype


-------------------+------+------+-------------------+--=
---------+------------+----------------+----------


         &n=
bsp;  100473 | 93   | 77   |   text1&nbs=
p;          | 0  &nb=
sp;          |  txt-pop3 | pop=
1_att           &nbs=
p;   | optional





Actual Output:



 content_object_id | xpos | ypos |   =
    text        | textang=
le |  texttype  |     symbol   =
;  | linktype


-------------------+------+------+-------------------+--=
---------+------------+----------------+----------


         &n=
bsp;  100473 | 93   |      | &=
nbsp;           &nbs=
p;     |        =
;   |          =
  |           &=
nbsp;    |


         &n=
bsp;  100473 |      | 77   | &=
nbsp;           &nbs=
p;     |        =
;   |          =
  |           &=
nbsp;    |


|

         &n=
bsp;  100473 |      |    =
  | text1          &=
nbsp; |           | =
           |  &=
nbsp;           &nbs=
p; |


         &n=
bsp;  100473 |      |    =
  |           &=
nbsp;       | 0     =
    |         &=
nbsp;  |          &n=
bsp;     |


         &n=
bsp;  100473 |      |    =
  |           &=
nbsp;       |     &n=
bsp;     | txt-pop3   |   &nbs=
p;            | T>

         &n=
bsp;  100473 |      |    =
  |           &=
nbsp;       |     &n=
bsp;     |       &nb=
sp;    |        &nbs=
p;        pop1_att|


SQL:





select distinct mapInfo.content_object_id,

        CASE WHEN map=
Info.node_id =3D 6957 THEN mapInfo.content END as xPos,


        CASE WHEN map=
Info.node_id =3D 6958 THEN mapInfo.content END as yPos,


        CASE WHEN map=
Info.node_id =3D 6959 THEN mapInfo.content END as text,


        CASE WHEN map=
Info.node_id =3D 6960 THEN mapInfo.content END as textAngle,


        CASE WHEN map=
Info.node_id =3D 6961 THEN mapInfo.content END as textType,


        CASE WHEN map=
Info.node_id =3D 6962 THEN mapInfo.content END as symbol,


        CASE WHEN map=
Info.node_id =3D 6963 THEN mapInfo.content END as linkType


from (  SELECT child_node_names.node_id, child_cont=
ent_facts.content_object_id,child_node_names.node_name,


            &nbs=
p;           CA=
SE WHEN child_node_names.node_id =3D 6962 THEN


            &nbs=
p;              =
;     (select node_name from node_names =
where node_id =3D content_fact_versions.content)


            &nbs=
p;           WH=
EN child_node_names.node_id =3D 6961 THEN


            &nbs=
p;              =
;     (select node_name from node_names =
where node_id =3D content_fact_versions.content)


            &nbs=
p;           WH=
EN child_node_names.node_id =3D 6963 THEN


            &nbs=
p;              =
;     (select node_name from node_names =
where node_id =3D content_fact_versions.content)


            &nbs=
p;           EL=
SE


            &nbs=
p;              =
;     content_fact_versions.content T>

            &nbs=
p;           EN=
D


            &nbs=
p;           FR=
OM content_objects_join


            &nbs=
p;           IN=
NER JOIN content_objects as parent_content_objects ON parent_content_object=
s.content_object_id =3D content_objects_join.parent_cobj_id



             =
;              =
     INNER JOIN nodes as parent_nodes ON=
parent_nodes.node_id =3D parent_content_objects.node_id



             =
;              =
             IZE=3D2>INNER JOIN node_names as parent_node_names ON parent_node_names.nod=
e_id =3D parent_nodes.node_id



             =
;           INN=
ER JOIN content_facts as child_content_facts ON child_content_facts.content=
_object_id =3D content_objects_join.child_cobj_id



             =
;              =
     INNER JOIN content_fact_versions ON=
content_fact_versions.fact_id =3D child_content_facts.fact_id



             =
;              =
     INNER JOIN node_names as child_node=
_names ON child_node_names.node_id =3D child_content_facts.node_id
P>

             =
;           WHE=
RE parent_nodes.node_id =3D 341


            &nbs=
p;           --=
AND parent_content_objects.object_type_id in (1,2,3,4,6,8,9)


            &nbs=
p;           AN=
D parent_content_objects.object_type_id =3D 73


            &nbs=
p;           OR=
DER BY child_content_facts.content_object_id


        ) as mapInfo =


group by mapInfo.content_object_id,  xPos, yPos, te=
xt, 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 in=
tended recipient of this email, you must not
use, copy or disclose its c=
ontent to anyone. You must not copy or
communicate to others content t=
hat is confidential or subject to
copyright, unless you have the consen=
t of the content owner.

------_=_NextPart_001_01C48322.1D7A5660--

Re: CROSS-TAB query help? I have read it cant be done in one

am 16.08.2004 10:06:08 von dev

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 (

) AS raw
GROUP BY content_object_id;

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: CROSS-TAB query help? I have read it cant be done in on

am 17.08.2004 04:47:12 von Theo.Galanakis

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C48404.7F6B2380
Content-Type: text/plain


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 (

) 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.
------_=_NextPart_001_01C48404.7F6B2380
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable






RE: [SQL] CROSS-TAB query help? I have read it cant be done in one S=<br /> QL, pro ve them wrong!



Thanks Rickard

Max may not work as not all the data is numerical. Howev=
er 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 i=
n ONE SQL without


> having to write a SP? The SQL at the end of this em=
ail attempts to


> display the subquery result-set in a cross-tab form=
at, 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-que=
ry 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      4=
0      50



> Actual Output:

>

>  content_object_id | xpos | ypos |  =
     text        | t=
extangle |  texttype


> |     symbol   &=
nbsp; | linktype


> -------------------+------+------+-----------------=
--+-----------+----


> -------------------+------+------+-----------------=
--+-----------+----


> -------------------+------+------+-----------------=
--+-----------+--


>        &nbs=
p;    100473 | 93   |     =
; |            =
       |      &=
nbsp;    |


>        &nbs=
p;    100473 |      | 77  =
; |            =
       |      &=
nbsp;    |


>        &nbs=
p;    100473 |      |  &n=
bsp;   | text1        &nb=
sp;   |         &nbs=
p; |



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 in=
tended recipient of this email, you must not
use, copy or disclose its c=
ontent to anyone. You must not copy or
communicate to others content t=
hat is confidential or subject to
copyright, unless you have the consen=
t of the content owner.

------_=_NextPart_001_01C48404.7F6B2380--

Re: CROSS-TAB query help? I have read it cant be done in on

am 17.08.2004 11:48:37 von dev

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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)