sql >> データベース >  >> RDS >> Sqlserver

複数の列の集計を含むSQLServerピボットテーブル

    UNPIVOTの両方を適用することで、これを少し異なる方法で実行します およびPIVOT 最終結果を取得するための関数。 ピボット解除 両方のtotalcountから値を取得します およびtotalamount 列を作成し、それらを複数の行を持つ1つの列に配置します。次に、それらの結果をピボットできます。:

    select chardate,
      Australia_totalcount as [Australia # of Transactions], 
      Australia_totalamount as [Australia Total $ Amount],
      Austria_totalcount as [Austria # of Transactions], 
      Austria_totalamount as [Austria Total $ Amount]
    from
    (
      select 
        numericmonth, 
        chardate,
        country +'_'+col col, 
        value
      from
      (
        select numericmonth, 
          country, 
          chardate,
          cast(totalcount as numeric(10, 2)) totalcount,
          cast(totalamount as numeric(10, 2)) totalamount
        from mytransactions
      ) src
      unpivot
      (
        value
        for col in (totalcount, totalamount)
      ) unpiv
    ) s
    pivot
    (
      sum(value)
      for col in (Australia_totalcount, Australia_totalamount,
                  Austria_totalcount, Austria_totalamount)
    ) piv
    order by numericmonth
    

    SQL FiddlewithDemoを参照してください。

    countryの数が不明な場合 名前の場合、動的SQLを使用できます:

    DECLARE @cols AS NVARCHAR(MAX),
        @colsName AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(country +'_'+c.col) 
                          from mytransactions
                          cross apply 
                          (
                            select 'TotalCount' col
                            union all
                            select 'TotalAmount'
                          ) c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsName 
        = STUFF((SELECT distinct ', ' + QUOTENAME(country +'_'+c.col) 
                   +' as ['
                   + country + case when c.col = 'TotalCount' then ' # of Transactions]' else 'Total $ Amount]' end
                 from mytransactions
                 cross apply 
                 (
                    select 'TotalCount' col
                    union all
                    select 'TotalAmount'
                 ) c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query 
      = 'SELECT chardate, ' + @colsName + ' 
         from 
         (
          select 
            numericmonth, 
            chardate,
            country +''_''+col col, 
            value
          from
          (
            select numericmonth, 
              country, 
              chardate,
              cast(totalcount as numeric(10, 2)) totalcount,
              cast(totalamount as numeric(10, 2)) totalamount
            from mytransactions
          ) src
          unpivot
          (
            value
            for col in (totalcount, totalamount)
          ) unpiv
         ) s
         pivot 
         (
           sum(value)
           for col in (' + @cols + ')
         ) p 
         order by numericmonth'
    
    execute(@query)
    

    SQL Fiddle with Demo

    を参照してください

    どちらも結果を出します:

    |             CHARDATE | AUSTRALIA # OF TRANSACTIONS | AUSTRALIA TOTAL $ AMOUNT | AUSTRIA # OF TRANSACTIONS | AUSTRIA TOTAL $ AMOUNT |
    --------------------------------------------------------------------------------------------------------------------------------------
    | Jul-12               |                          36 |                   699.96 |                        11 |                 257.82 |
    | Aug-12               |                          44 |                  1368.71 |                         5 |                 126.55 |
    | Sep-12               |                          52 |                  1161.33 |                         7 |                  92.11 |
    | Oct-12               |                          50 |                  1099.84 |                        12 |                 103.56 |
    | Nov-12               |                          38 |                  1078.94 |                        21 |                 377.68 |
    | Dec-12               |                          63 |                  1668.23 |                         3 |                  14.35 |
    


    1. MySQLを使用した基本的なSQLクエリについて学ぶ

    2. Debian 5のphpMyAdminでMySQLを管理する(Lenny)

    3. トランザクション内(SQL Server内)で複数のDDLステートメントを実行することは可能ですか?

    4. HikariCPPostgresqlドライバーがJDBCURLを受け入れないと主張