Announcement

Collapse
No announcement yet.

SQL Transponieren

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQL Transponieren

    hallo,

    ich habe folgendes Problem:

    diese Tabelle:

    Artikel | Attribute
    1 | a
    1 | b
    1 | c
    2 | x
    3 | y
    3 | z
    ........

    muss ich itgendwie in dese Stuktur bekommen:

    Artikel | Attribute1 | Attribute2 | Attribute3
    1 | a | b | c
    2 | x | NULL | NULL
    3 | y | z | NULL
    ......

    hat da jemand eine Lösung??

    danke, hape
    Zuletzt editiert von weinhape; 20.07.2009, 16:53.

  • #2
    Originally posted by weinhape View Post
    hallo,

    ich habe folgendes Problem:

    diese Tabelle:

    Artikel | Attribute
    1 | a
    1 | b
    1 | c
    2 | x
    3 | y
    3 | z
    ........

    muss ich itgendwie in dese Stuktur bekommen:

    Artikel | Attribute1 | Attribute2 | Attribute3
    1 | a | b | c
    2 | x | NULL | NULL
    3 | y | z | NULL
    ......

    hat da jemand eine Lösung??

    danke, hape
    Hallo,

    Welche DB?

    Comment


    • #3
      da ich es für eine Datenmigtation brauche ist es egal ob ich es in einer MS-SQL-2005 Server oder in der Quelle (eine oracle DB) mache....

      hauptsache es funzt... egal wo... :-)

      danke im voraus

      Comment


      • #4
        Originally posted by weinhape View Post
        da ich es für eine Datenmigtation brauche ist es egal ob ich es in einer MS-SQL-2005 Server oder in der Quelle (eine oracle DB) mache....

        hauptsache es funzt... egal wo... :-)

        danke im voraus
        ok, in Oracle kannst du es folgendermassen machen (ab 9iR2)

        Code:
        
        select
           artikel,
           xmltransform
           (
              sys_xmlagg
              (
                 sys_xmlgen(attribute)
              ),
             xmltype
             (
               '<?xml version="1.0"?><xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                  <xsl:template match="/">
                    <xsl:for-each select="/ROWSET/ATTRIBUTE">
                      <xsl:value-of select="text()"/>;</xsl:for-each>
                  </xsl:template>
                </xsl:stylesheet>'
             )
          ).getstringval() listagg
        from MyTable
        group by artikel;

        Gruss

        Comment


        • #5
          hallo das sieht gut aus, darum habe ich das gleich in mein original SQL eingebaut....
          nun habe ich aber ein Problem "listagg" liefert mir nur NULL:

          select
          MSB.SEGMENT1,
          --mtc.item_catalog_group_id,
          xmltransform
          (
          sys_xmlagg
          (
          sys_xmlgen(mde.element_name)
          ),
          xmltype
          (
          '<?xml version="1.0"?><xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">
          <xsl:for-each select="/ROWSET/mde.element_name">
          <xsl:value-of select="text()"/>;</xsl:for-each>
          </xsl:template>
          </xsl:stylesheet>'
          )
          ).getstringval() listagg
          FROM apps.MTL_DESCRIPTIVE_ELEMENTS MDE
          ,apps.MTL_ITEM_CATALOG_GROUPS_B_KFV MTC
          ,apps.MTL_SYSTEM_ITEMS_FVL MSB
          WHERE MDE.ITEM_CATALOG_GROUP_ID=MTC.ITEM_CATALOG_GROUP_I D
          AND MTC.ITEM_CATALOG_GROUP_ID=MSB.ITEM_CATALOG_GROUP_I D
          and inventory_item_id in (
          select d.inventory_item_id FROM apps.bom_inventory_components a,
          apps.bom_bill_of_materials b,
          apps.mtl_system_items_b c,
          apps.mtl_system_items_b d
          WHERE b.bill_sequence_id = a.bill_sequence_id
          AND b.organization_id = 122
          AND b.assembly_item_id = c.inventory_item_id
          AND c.organization_id = b.organization_id
          AND a.component_item_id= d.inventory_item_id
          AND d.organization_id = b.organization_id
          START WITH a.bill_sequence_id = (select bill_Sequence_id from apps.bom_bill_of_materials where assembly_item_id =
          (select inventory_item_id from apps.mtl_system_items_b where segment1 = '132020000'and
          organization_id = 122)
          and organization_id = 122)
          CONNECT BY PRIOR a.component_item_id = b.assembly_item_id)
          group by MSB.SEGMENT1

          was hab ich da falsch??

          Comment


          • #6
            Originally posted by weinhape View Post
            hallo das sieht gut aus, darum habe ich das gleich in mein original SQL eingebaut....
            nun habe ich aber ein Problem "listagg" liefert mir nur NULL:

            select
            MSB.SEGMENT1,
            --mtc.item_catalog_group_id,
            xmltransform
            (
            sys_xmlagg
            (
            sys_xmlgen(mde.element_name)
            ),
            xmltype
            (
            '<?xml version="1.0"?><xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="/">
            <xsl:for-each select="/ROWSET/mde.element_name">
            <xsl:value-of select="text()"/>;</xsl:for-each>
            </xsl:template>
            </xsl:stylesheet>'
            )
            ).getstringval() listagg
            FROM apps.MTL_DESCRIPTIVE_ELEMENTS MDE
            ,apps.MTL_ITEM_CATALOG_GROUPS_B_KFV MTC
            ,apps.MTL_SYSTEM_ITEMS_FVL MSB
            WHERE MDE.ITEM_CATALOG_GROUP_ID=MTC.ITEM_CATALOG_GROUP_I D
            AND MTC.ITEM_CATALOG_GROUP_ID=MSB.ITEM_CATALOG_GROUP_I D
            and inventory_item_id in (
            select d.inventory_item_id FROM apps.bom_inventory_components a,
            apps.bom_bill_of_materials b,
            apps.mtl_system_items_b c,
            apps.mtl_system_items_b d
            WHERE b.bill_sequence_id = a.bill_sequence_id
            AND b.organization_id = 122
            AND b.assembly_item_id = c.inventory_item_id
            AND c.organization_id = b.organization_id
            AND a.component_item_id= d.inventory_item_id
            AND d.organization_id = b.organization_id
            START WITH a.bill_sequence_id = (select bill_Sequence_id from apps.bom_bill_of_materials where assembly_item_id =
            (select inventory_item_id from apps.mtl_system_items_b where segment1 = '132020000'and
            organization_id = 122)
            and organization_id = 122)
            CONNECT BY PRIOR a.component_item_id = b.assembly_item_id)
            group by MSB.SEGMENT1

            was hab ich da falsch??

            uiiii :-)

            - Das reine Pivotisieren auf das fragliche Attribut funktioniert aber , d.h. wenn du das Aggregieren ohne den "Rest" laufenlässt ?


            Gruss

            Comment


            • #7
              hi habs nun auf den katalog eingeschränkt und versucht dessen "element_name" - Felder zu transponieren...
              funzt auch nicht....

              hier mein SQL:
              select
              item_catalog_group_id,
              xmltransform
              (
              sys_xmlagg
              (
              sys_xmlgen(element_name)
              ),
              xmltype
              (
              '<?xml version="1.0"?><xsl:stylesheet version="1.0"
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
              <xsl:template match="/">
              <xsl:for-each select="/ROWSET/ATTRIBUTE">
              <xsl:value-of select="text()"/>;</xsl:for-each>
              </xsl:template>
              </xsl:stylesheet>'
              )
              ).getstringval() listagg
              FROM apps.MTL_DESCRIPTIVE_ELEMENTS
              --where item_catalog_group_id = 122
              group by item_catalog_group_id

              aber erst mal danke für deine schnellen antwworten.... echt genial!!

              Comment


              • #8
                Originally posted by weinhape View Post
                hi habs nun auf den katalog eingeschränkt und versucht dessen "element_name" - Felder zu transponieren...
                funzt auch nicht....

                hier mein SQL:
                select
                item_catalog_group_id,
                xmltransform
                (
                sys_xmlagg
                (
                sys_xmlgen(element_name)
                ),
                xmltype
                (
                '<?xml version="1.0"?><xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:template match="/">
                <xsl:for-each select="/ROWSET/ATTRIBUTE">
                <xsl:value-of select="text()"/>;</xsl:for-each>
                </xsl:template>
                </xsl:stylesheet>'
                )
                ).getstringval() listagg
                FROM apps.MTL_DESCRIPTIVE_ELEMENTS
                --where item_catalog_group_id = 122
                group by item_catalog_group_id

                aber erst mal danke für deine schnellen antwworten.... echt genial!!
                Hier ist etwas schief :

                Code:
                 <xsl:for-each select="/ROWSET/ATTRIBUTE">
                - Dass "ATTRIBUTE" muss durch "element_name" ersetzt werden


                Gruss

                Comment


                • #9
                  hatte ich schon, dann ging es auch nicht....
                  ich bin aber drauf gekommen dass es da Key-sensitive ist!!

                  recht herzlichen Dank für die Hilfe..... nun haut es hin!!

                  hape

                  Comment


                  • #10
                    Originally posted by weinhape View Post
                    ich bin aber drauf gekommen dass es da Key-sensitive ist!!

                    hape
                    Stimmt, habe ich nicht daran gedacht, ansonsten ist Oracle ja eher "case-insensitive", aber sobald du in der XML Domaine bist, dann schon, klar


                    Gruss

                    Comment


                    • #11
                      jetz hab ich nur das problem, wie ich die wurst, die entsteht in einzelne felder aufteile...

                      Comment


                      • #12
                        hab ne lösung gefunden!!!:


                        CREATE FUNCTION [dbo].[splitfunction]
                        (
                        @Position INTEGER,
                        @List VARCHAR(2000),
                        @Delimiter VARCHAR(1)
                        )
                        RETURNS VARCHAR(2000)
                        AS
                        BEGIN
                        DECLARE @RetVal VARCHAR(2000) SET @RetVal = ''
                        DECLARE @Counter INT SET @Counter = 0
                        DECLARE @StartPos INT
                        DECLARE @Length INT
                        DECLARE @DelimPos INT SET @DelimPos = 0

                        WHILE @Counter < @Position

                        BEGIN
                        SET @Counter = @Counter + 1 -- zaehler
                        SET @StartPos = @DelimPos + 1 -- startposition
                        SET @DelimPos = CHARINDEX(@Delimiter,@List,@DelimPos + 1) --nächsten Delimiter finden

                        IF @DelimPos = 0
                        BEGIN
                        IF @Counter = @Position SET @Length = (LEN(@List)+1)-@StartPos
                        ELSE SET @Length = -1
                        END
                        ELSE SET @Length = @DelimPos - @StartPos
                        END

                        -- Rückgabe des Strings
                        IF @Length > 0 SET @RetVal = SUBSTRING(@List,@StartPos,@Length)
                        RETURN @RetVal
                        END

                        Comment


                        • #13
                          noch ein problem ist gerade aufgekommen...
                          in der originaltabelle stehen dioe werte folgendermasen drinnen:

                          ART
                          NORM
                          ANSCHL
                          [MM] DN
                          [MM] LANG
                          MAT
                          DESCRIPTION

                          aus dem Transform kommt es aber alfabethisch geordnet raus:
                          ANSCHL;ART;DESCRIPTION;NORM;[MM] LANG;[MM] DN;MAT;

                          wie kann ich das verhindern??

                          Comment

                          Working...
                          X