none
Problem beim Aggregieren/Pivotieren RRS feed

  • Frage

  • Hallo zusammen,

    ich habe eine Tabelle, in der die Werte einer Spalte in Bezug auf eine andere Spalte transponiert werden sollen:

    CREATE TABLE [dbo].[TableTest](
    	[ID] [nchar](10) NOT NULL,
    	[Produkt] [nvarchar](10) NULL,
    	[Unterprodukt] [nvarchar](10) NULL
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[TableTest] ([ID], [Produkt], [Unterprodukt]) VALUES (N'1', N'P1', N'UP1')
    INSERT [dbo].[TableTest] ([ID], [Produkt], [Unterprodukt]) VALUES (N'2', N'P1', N'UP2')
    INSERT [dbo].[TableTest] ([ID], [Produkt], [Unterprodukt]) VALUES (N'3', N'P2', N'UP3')
    INSERT [dbo].[TableTest] ([ID], [Produkt], [Unterprodukt]) VALUES (N'4', N'P2', N'UP4')
    INSERT [dbo].[TableTest] ([ID], [Produkt], [Unterprodukt]) VALUES (N'5', N'P2', N'UP5')

    Die obige GrundTabelle ist ein Ergebnis einer Abfrage unter Nutzung einer cte.

    Als Ergebnis erwarte ich:

    Wer kann mir da auf die Sprünge helfen? 

    Gruß Jürgen

    Freitag, 19. Oktober 2018 12:25

Antworten

  • Hallo Jürgen

    Als Sprungbrett mal dies:

    CREATE TABLE dbo.TableTest(
    	ID nchar(10) NOT NULL,
    	Produkt nvarchar(10) NULL,
    	Unterprodukt nvarchar(10) NULL
    )
    GO
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'1', N'P1', N'UP1')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'2', N'P1', N'UP2')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'3', N'P2', N'UP3')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'4', N'P2', N'UP4')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'5', N'P2', N'UP5')
    go
    
    
    DECLARE @colsPivot AS NVARCHAR(MAX),@colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    ;
    -- concatenate all columns to be pivoted as string-list
    select @colsUnpivot = 'Unterprodukt'
    ;
    -- label the unpivoting columns
    with colPiv as
    (
    select distinct 'Unterprodukt'+convert(varchar,row_number() over (partition by Produkt order by (select null))) UPLabel
    from dbo.TableTest
    )
    select @colsPivot=string_agg(UPLabel,', ')
    from colPiv
    ;
    -- dynamic query
    set @query=
    'select *
    from
    (
      select Produkt,value, col+RN AS ColumnLabel, [Anzahl UP]
      from
      (
        select 
          Produkt
        , Unterprodukt
        , convert(varchar,row_number() over (partition by Produkt order by (select null))) RN
        , count(Unterprodukt) over(partition by Produkt) [Anzahl UP]
        from dbo.TableTest
      ) src
      unpivot ( value for col in ('+@colsUnpivot+')) up
    ) un
    pivot ( min(value) for columnLabel in ('+@colsPivot+')) piv
    '
    ;
    exec (@query)
    GO
    

    Gruss

    Blaise.

    • Als Antwort markiert Jürgen Sch Donnerstag, 25. Oktober 2018 13:09
    Donnerstag, 25. Oktober 2018 05:37

Alle Antworten

  • Hallo Jürgen

    Als Sprungbrett mal dies:

    CREATE TABLE dbo.TableTest(
    	ID nchar(10) NOT NULL,
    	Produkt nvarchar(10) NULL,
    	Unterprodukt nvarchar(10) NULL
    )
    GO
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'1', N'P1', N'UP1')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'2', N'P1', N'UP2')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'3', N'P2', N'UP3')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'4', N'P2', N'UP4')
    INSERT dbo.TableTest (ID, Produkt, Unterprodukt) VALUES (N'5', N'P2', N'UP5')
    go
    
    
    DECLARE @colsPivot AS NVARCHAR(MAX),@colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    ;
    -- concatenate all columns to be pivoted as string-list
    select @colsUnpivot = 'Unterprodukt'
    ;
    -- label the unpivoting columns
    with colPiv as
    (
    select distinct 'Unterprodukt'+convert(varchar,row_number() over (partition by Produkt order by (select null))) UPLabel
    from dbo.TableTest
    )
    select @colsPivot=string_agg(UPLabel,', ')
    from colPiv
    ;
    -- dynamic query
    set @query=
    'select *
    from
    (
      select Produkt,value, col+RN AS ColumnLabel, [Anzahl UP]
      from
      (
        select 
          Produkt
        , Unterprodukt
        , convert(varchar,row_number() over (partition by Produkt order by (select null))) RN
        , count(Unterprodukt) over(partition by Produkt) [Anzahl UP]
        from dbo.TableTest
      ) src
      unpivot ( value for col in ('+@colsUnpivot+')) up
    ) un
    pivot ( min(value) for columnLabel in ('+@colsPivot+')) piv
    '
    ;
    exec (@query)
    GO
    

    Gruss

    Blaise.

    • Als Antwort markiert Jürgen Sch Donnerstag, 25. Oktober 2018 13:09
    Donnerstag, 25. Oktober 2018 05:37
  • Hallo Blaise,

    danke für deine Antwort und deinen Lösungsvorschlag.

    Vom Ergebnis ist es dass, was ich gesucht habe. Ich hatte bereits eine eigene Lösung probiert, indem ich statt des Pivots mehrere geschachtelte Unterabfragen erzeugt habe.

    Ich werde deinen Lösungsvorschlag mal in unserer Produktivumgebung umsetzen und testen.

    Bis dann

    Gruß Jürgen

    Donnerstag, 25. Oktober 2018 13:09