• <tfoot id='btVYs'></tfoot>

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

        <i id='btVYs'><tr id='btVYs'><dt id='btVYs'><q id='btVYs'><span id='btVYs'><b id='btVYs'><form id='btVYs'><ins id='btVYs'></ins><ul id='btVYs'></ul><sub id='btVYs'></sub></form><legend id='btVYs'></legend><bdo id='btVYs'><pre id='btVYs'><center id='btVYs'></center></pre></bdo></b><th id='btVYs'></th></span></q></dt></tr></i><div id='btVYs'><tfoot id='btVYs'></tfoot><dl id='btVYs'><fieldset id='btVYs'></fieldset></dl></div>
        • <bdo id='btVYs'></bdo><ul id='btVYs'></ul>
        <legend id='btVYs'><style id='btVYs'><dir id='btVYs'><q id='btVYs'></q></dir></style></legend>
      1. sql 2005 中的枢轴

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

            • <tfoot id='S07Hz'></tfoot>
                <tbody id='S07Hz'></tbody>

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

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

                  本文介绍了sql 2005 中的枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要旋转一列(数字列).示例需要此数据:

                  I need to pivot one column (Numbers column). example need this data:

                  a 1
                  a 2
                  b 3
                  b 4
                  c 5
                  d 6
                  d 7
                  d 8
                  d 9
                  e 10
                  e 11
                  e 12
                  e 13
                  e 14
                  

                  看起来像这样

                  a 1 2
                  b 3 4
                  c 5
                  d 6 7 8 9
                  e 10 11 12 13 14
                  

                  任何帮助将不胜感激...

                  any help would be greatly appreciated...

                  推荐答案

                  使用 ROW_NUMBER()PIVOT 和一些动态 SQL(但不需要游标):

                  Using ROW_NUMBER(), PIVOT and some dynamic SQL (but no cursor necessary) :

                  CREATE TABLE [dbo].[stackoverflow_198716](
                      [code] [varchar](1) NOT NULL,
                      [number] [int] NOT NULL
                  ) ON [PRIMARY]
                  
                  DECLARE @sql AS varchar(max)
                  DECLARE @pivot_list AS varchar(max) -- Leave NULL for COALESCE technique
                  DECLARE @select_list AS varchar(max) -- Leave NULL for COALESCE technique
                  
                  SELECT @pivot_list = COALESCE(@pivot_list + ', ', '') + '[' + CONVERT(varchar, PIVOT_CODE) + ']'
                          ,@select_list = COALESCE(@select_list + ', ', '') + '[' + CONVERT(varchar, PIVOT_CODE) + '] AS [col_' + CONVERT(varchar, PIVOT_CODE) + ']'
                  FROM (
                      SELECT DISTINCT PIVOT_CODE
                      FROM (
                          SELECT code, number, ROW_NUMBER() OVER (PARTITION BY code ORDER BY number) AS PIVOT_CODE
                          FROM stackoverflow_198716
                      ) AS rows
                  ) AS PIVOT_CODES
                  
                  SET @sql = '
                  ;WITH p AS (
                      SELECT code, number, ROW_NUMBER() OVER (PARTITION BY code ORDER BY number) AS PIVOT_CODE
                      FROM stackoverflow_198716
                  )
                  SELECT code, ' + @select_list + '
                  FROM p
                  PIVOT (
                      MIN(number)
                      FOR PIVOT_CODE IN (
                          ' + @pivot_list + '
                      )
                  ) AS pvt
                  '
                  
                  PRINT @sql
                  
                  EXEC (@sql)
                  

                  这篇关于sql 2005 中的枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server 中的动态枢轴列 下一篇:暂时关闭约束 (MS SQL)

                  相关文章

                  <tfoot id='mfeGT'></tfoot>
                  <legend id='mfeGT'><style id='mfeGT'><dir id='mfeGT'><q id='mfeGT'></q></dir></style></legend>
                    <bdo id='mfeGT'></bdo><ul id='mfeGT'></ul>

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

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