X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A2A525E46C0 for ; Sun, 15 Aug 2004 20:48:47 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 40482-10 for ; Sun, 15 Aug 2004 23:48:42 +0000 (GMT) Received: from angel.lonelyplanet.com.au (mail.lonelyplanet.com.au [202.147.44.168]) by svr1.postgresql.org (Postfix) with ESMTP id 2715F5E46BF for ; Sun, 15 Aug 2004 20:48:38 -0300 (ADT) Received: from shiva.au.lpint.net ([192.168.61.22]) by angel with InterScan Messaging Security Suite; Mon, 16 Aug 2004 09:35:50 +1000 Received: by shiva.au.lpint.net with Internet Mail Service (5.5.2656.59) id ; Mon, 16 Aug 2004 09:46:43 +1000 Message-ID: <82E30406384FFB44AFD1012BAB230B55037D0504@shiva.au.lpint.net> From: Theo Galanakis To: "'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 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2656.59) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C48322.1D7A5660" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.9 tagged_above=0.0 required=5.0 tests=EXCUSE_16, HTML_40_50, HTML_MESSAGE, PLING_QUERY X-Spam-Level: X-Archive-Number: 200408/139 X-Sequence-Number: 18636 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 = 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.

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;            |
         &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
            &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

             = ;              =              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

             = ;           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--