<small id='04TCi'></small><noframes id='04TCi'>

    1. <tfoot id='04TCi'></tfoot>
    2. <i id='04TCi'><tr id='04TCi'><dt id='04TCi'><q id='04TCi'><span id='04TCi'><b id='04TCi'><form id='04TCi'><ins id='04TCi'></ins><ul id='04TCi'></ul><sub id='04TCi'></sub></form><legend id='04TCi'></legend><bdo id='04TCi'><pre id='04TCi'><center id='04TCi'></center></pre></bdo></b><th id='04TCi'></th></span></q></dt></tr></i><div id='04TCi'><tfoot id='04TCi'></tfoot><dl id='04TCi'><fieldset id='04TCi'></fieldset></dl></div>
        <bdo id='04TCi'></bdo><ul id='04TCi'></ul>
    3. <legend id='04TCi'><style id='04TCi'><dir id='04TCi'><q id='04TCi'></q></dir></style></legend>
      1. 在sql server中高效地将行转换为列

        Efficiently convert rows to columns in sql server(在sql server中高效地将行转换为列)

            <small id='9WuzN'></small><noframes id='9WuzN'>

            <tfoot id='9WuzN'></tfoot>
            • <bdo id='9WuzN'></bdo><ul id='9WuzN'></ul>
            • <i id='9WuzN'><tr id='9WuzN'><dt id='9WuzN'><q id='9WuzN'><span id='9WuzN'><b id='9WuzN'><form id='9WuzN'><ins id='9WuzN'></ins><ul id='9WuzN'></ul><sub id='9WuzN'></sub></form><legend id='9WuzN'></legend><bdo id='9WuzN'><pre id='9WuzN'><center id='9WuzN'></center></pre></bdo></b><th id='9WuzN'></th></span></q></dt></tr></i><div id='9WuzN'><tfoot id='9WuzN'></tfoot><dl id='9WuzN'><fieldset id='9WuzN'></fieldset></dl></div>

                  <tbody id='9WuzN'></tbody>
                <legend id='9WuzN'><style id='9WuzN'><dir id='9WuzN'><q id='9WuzN'></q></dir></style></legend>
                  本文介绍了在sql server中高效地将行转换为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一种有效的方法在SQL Server中将行转换为列,听说PIVOT不是很快,我需要处理大量记录.

                  I'm looking for an efficient way to convert rows to columns in SQL server, I heard that PIVOT is not very fast, and I need to deal with lot of records.

                  这是我的例子:

                     -------------------------------
                     | Id | Value  | ColumnName    |
                     -------------------------------
                     | 1  | John   | FirstName     |
                     | 2  | 2.4    | Amount        |
                     | 3  | ZH1E4A | PostalCode    |
                     | 4  | Fork   | LastName      |
                     | 5  | 857685 | AccountNumber |
                     -------------------------------
                  

                  这是我的结果:

                  ---------------------------------------------------------------------
                  | FirstName  |Amount|   PostalCode   |   LastName  |  AccountNumber |
                  ---------------------------------------------------------------------
                  | John       | 2.4  |   ZH1E4A       |   Fork      |  857685        |
                  ---------------------------------------------------------------------
                  

                  如何构建结果?

                  推荐答案

                  有多种方法可以将数据从多行转换为列.

                  There are several ways that you can transform data from multiple rows into columns.

                  在 SQL Server 中,您可以使用 PIVOT 函数将数据从行转换为列:

                  In SQL Server you can use the PIVOT function to transform the data from rows to columns:

                  select Firstname, Amount, PostalCode, LastName, AccountNumber
                  from
                  (
                    select value, columnname
                    from yourtable
                  ) d
                  pivot
                  (
                    max(value)
                    for columnname in (Firstname, Amount, PostalCode, LastName, AccountNumber)
                  ) piv;
                  

                  参见演示.

                  如果您想转置未知数量的columnnames,那么您可以使用动态SQL:

                  If you have an unknown number of columnnames that you want to transpose, then you can use dynamic SQL:

                  DECLARE @cols AS NVARCHAR(MAX),
                      @query  AS NVARCHAR(MAX)
                  
                  select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName) 
                                      from yourtable
                                      group by ColumnName, id
                                      order by id
                              FOR XML PATH(''), TYPE
                              ).value('.', 'NVARCHAR(MAX)') 
                          ,1,1,'')
                  
                  set @query = N'SELECT ' + @cols + N' from 
                               (
                                  select value, ColumnName
                                  from yourtable
                              ) x
                              pivot 
                              (
                                  max(value)
                                  for ColumnName in (' + @cols + N')
                              ) p '
                  
                  exec sp_executesql @query;
                  

                  参见演示.

                  如果不想使用PIVOT函数,那么可以使用带有CASE表达式的聚合函数:

                  If you do not want to use the PIVOT function, then you can use an aggregate function with a CASE expression:

                  select
                    max(case when columnname = 'FirstName' then value end) Firstname,
                    max(case when columnname = 'Amount' then value end) Amount,
                    max(case when columnname = 'PostalCode' then value end) PostalCode,
                    max(case when columnname = 'LastName' then value end) LastName,
                    max(case when columnname = 'AccountNumber' then value end) AccountNumber
                  from yourtable
                  

                  参见演示.

                  这也可以使用多个连接来完成,但您需要一些列来关联示例数据中没有的每一行.但基本语法是:

                  This could also be completed using multiple joins, but you will need some column to associate each of the rows which you do not have in your sample data. But the basic syntax would be:

                  select fn.value as FirstName,
                    a.value as Amount,
                    pc.value as PostalCode,
                    ln.value as LastName,
                    an.value as AccountNumber
                  from yourtable fn
                  left join yourtable a
                    on fn.somecol = a.somecol
                    and a.columnname = 'Amount'
                  left join yourtable pc
                    on fn.somecol = pc.somecol
                    and pc.columnname = 'PostalCode'
                  left join yourtable ln
                    on fn.somecol = ln.somecol
                    and ln.columnname = 'LastName'
                  left join yourtable an
                    on fn.somecol = an.somecol
                    and an.columnname = 'AccountNumber'
                  where fn.columnname = 'Firstname'
                  

                  这篇关于在sql server中高效地将行转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Select n random rows from SQL Server table(从 SQL Server 表中随机选择 n 行)
                  SQL query to select dates between two dates(用于选择两个日期之间的日期的 SQL 查询)
                  How can I delete using INNER JOIN with SQL Server?(如何在 SQL Server 中使用 INNER JOIN 进行删除?)
                  Table Naming Dilemma: Singular vs. Plural Names(表命名困境:单数与复数名称)
                  INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server(INSERT 语句与 FOREIGN KEY 约束冲突 - SQL Server)
                  How ROWNUM works in pagination query?(ROWNUM 如何在分页查询中工作?)

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

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

                      <tbody id='A6tcx'></tbody>
                          <tfoot id='A6tcx'></tfoot>
                          • <bdo id='A6tcx'></bdo><ul id='A6tcx'></ul>
                            <legend id='A6tcx'><style id='A6tcx'><dir id='A6tcx'><q id='A6tcx'></q></dir></style></legend>