<i id='WxXma'><tr id='WxXma'><dt id='WxXma'><q id='WxXma'><span id='WxXma'><b id='WxXma'><form id='WxXma'><ins id='WxXma'></ins><ul id='WxXma'></ul><sub id='WxXma'></sub></form><legend id='WxXma'></legend><bdo id='WxXma'><pre id='WxXma'><center id='WxXma'></center></pre></bdo></b><th id='WxXma'></th></span></q></dt></tr></i><div id='WxXma'><tfoot id='WxXma'></tfoot><dl id='WxXma'><fieldset id='WxXma'></fieldset></dl></div>
    1. <small id='WxXma'></small><noframes id='WxXma'>

        <tfoot id='WxXma'></tfoot>
          <bdo id='WxXma'></bdo><ul id='WxXma'></ul>

      1. <legend id='WxXma'><style id='WxXma'><dir id='WxXma'><q id='WxXma'></q></dir></style></legend>
      2. 具有多列聚合的 SQL Server 数据透视表

        时间:2023-07-17
        <i id='MVeY3'><tr id='MVeY3'><dt id='MVeY3'><q id='MVeY3'><span id='MVeY3'><b id='MVeY3'><form id='MVeY3'><ins id='MVeY3'></ins><ul id='MVeY3'></ul><sub id='MVeY3'></sub></form><legend id='MVeY3'></legend><bdo id='MVeY3'><pre id='MVeY3'><center id='MVeY3'></center></pre></bdo></b><th id='MVeY3'></th></span></q></dt></tr></i><div id='MVeY3'><tfoot id='MVeY3'></tfoot><dl id='MVeY3'><fieldset id='MVeY3'></fieldset></dl></div>
          <tbody id='MVeY3'></tbody>

        <small id='MVeY3'></small><noframes id='MVeY3'>

          • <legend id='MVeY3'><style id='MVeY3'><dir id='MVeY3'><q id='MVeY3'></q></dir></style></legend>
              • <bdo id='MVeY3'></bdo><ul id='MVeY3'></ul>

                1. <tfoot id='MVeY3'></tfoot>

                  本文介绍了具有多列聚合的 SQL Server 数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一张桌子:

                   create table mytransactions(country varchar(30), totalcount int, numericmonth int, chardate char(20), totalamount money)
                  

                  该表有以下记录:

                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 36, 7, 'Jul-12', 699.96)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 44, 8, 'Aug-12', 1368.71)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 52, 9, 'Sep-12', 1161.33)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 50, 10, 'Oct-12', 1099.84)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 38, 11, 'Nov-12', 1078.94)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Australia', 63, 12, 'Dec-12', 1668.23)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 11, 7, 'Jul-12', 257.82)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 5, 8, 'Aug-12', 126.55)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 7, 9, 'Sep-12', 92.11)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 12, 10, 'Oct-12', 103.56)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 21, 11, 'Nov-12', 377.68)
                  Go
                  insert into mytransactions(country, totalcount, numericmonth, chardate, totalamount) values('Austria', 3, 12, 'Dec-12', 14.35)
                  Go
                  

                  这是 select * 的样子:

                  This is what a select * looks like:

                  Country         TotalCount numericmonth  chardate totalamount
                  ---------       ---------- -----------   -------- -----------
                  Australia       36         7             Jul-12   699.96
                  Australia       44         8             Aug-12   1368.71
                  Australia       52         9             Sep-12   1161.33
                  Australia       50         10            Oct-12   1099.84
                  Australia       38         11            Nov-12   1078.94
                  Australia       63         12            Dec-12   1668.23
                  Austria         11         7             Jul-12   257.82
                  Austria          5         8             Aug-12   126.55
                  Austria          7         9             Sep-12   92.11
                  Austria         12         10            Oct-12   103.56
                  Austria         21         11            Nov-12   377.68
                  Austria          3         12            Dec-12   14.35
                  

                  我想旋转这个记录集,使它看起来像这样:

                  I want to pivot this record set so it looks like this:

                                     Australia          Australia        Austria              Austria
                                     # of Transactions  Total $ amount   # of Transactions    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
                  

                  这是迄今为止我想出的数据透视代码:

                  This is the pivot code I've come up with so far:

                  select * from  mytransactions
                  pivot (sum (totalcount) for country in ([Australia], [Austria])) as pvt
                  

                  这是我得到的:

                  numericmonth     chardate     totalamount     Australia   Austria
                  -----------      --------     ----------      ---------   -------
                  7                Jul-12       257.82          NULL        11
                  7                Jul-12       699.96          36          NULL
                  8                Aug-12       126.55          NULL        5
                  8                Aug-12       1368.71         44          NULL
                  9                Sep-12       92.11           NULL        7
                  9                Sep-12       1161.33         52          NULL
                  10               Oct-12       103.56          NULL        12
                  10               Oct-12       1099.84         50          NULL
                  11               Nov-12       377.68          NULL        21
                  11               Nov-12       1078.94         38          NULL
                  12               Dec-12       14.35           NULL        3
                  12               Dec-12       1668.23         63          NULL
                  

                  我可以手动聚合表变量循环中的记录,但是似乎pivot 可以做到这一点.

                  I can manually aggregate the records in a table variable loop, however it seems that pivot might be able to do this.

                  是否可以使用pivot获取我想要的记录集,或者还有其他我不知道的工具吗?

                  Is is possible to get the record set I want using pivot or is there another tool that I'm not aware of?

                  谢谢

                  推荐答案

                  我会通过应用 UNPIVOTPIVOT 函数来获得最终结果结果.unpivottotalcounttotalamount 列中获取值,并将它们放入具有多行的一列中.然后,您可以根据这些结果进行调整.:

                  I would do this slightly different by applying both the UNPIVOT and the PIVOT functions to get the final result. The unpivot takes the values from both the totalcount and totalamount columns and places them into one column with multiple rows. You can then pivot on those results.:

                  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 Fiddle with Demo.

                  如果你有未知数量的country名称,那么你可以使用动态SQL:

                  If you have an unknown number of country names, then you can use dynamic 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 |
                  

                  这篇关于具有多列聚合的 SQL Server 数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server 检查区分大小写? 下一篇:T-SQL:四舍五入到最接近的 15 分钟间隔

                  相关文章

                    <small id='53cPC'></small><noframes id='53cPC'>

                  1. <legend id='53cPC'><style id='53cPC'><dir id='53cPC'><q id='53cPC'></q></dir></style></legend>

                      <bdo id='53cPC'></bdo><ul id='53cPC'></ul>
                  2. <tfoot id='53cPC'></tfoot>
                    <i id='53cPC'><tr id='53cPC'><dt id='53cPC'><q id='53cPC'><span id='53cPC'><b id='53cPC'><form id='53cPC'><ins id='53cPC'></ins><ul id='53cPC'></ul><sub id='53cPC'></sub></form><legend id='53cPC'></legend><bdo id='53cPC'><pre id='53cPC'><center id='53cPC'></center></pre></bdo></b><th id='53cPC'></th></span></q></dt></tr></i><div id='53cPC'><tfoot id='53cPC'></tfoot><dl id='53cPC'><fieldset id='53cPC'></fieldset></dl></div>