【问题标题】:Converting Columns Into Rows Using Pivot In SQL Server在 SQL Server 中使用 Pivot 将列转换为行
【发布时间】:2017-03-21 11:59:02
【问题描述】:

我有如下表格:

我想将其转换如下:

+------------+---------------------+---------------------+
| Child_Code |     SewingStart     |      SewingEnd      |
+------------+---------------------+---------------------+
|     000001 | 2017-02-21 00:00:00 | 2017-03-21 00:00:00 |
+------------+---------------------+---------------------+

请帮忙!!

【问题讨论】:

标签: sql sql-server pivot pivot-table


【解决方案1】:

如果行数有限,可以使用条件聚合或数据透视。但是,你需要一个专栏。所以:

select child_code,
       max(case when seqnum = 1 then plan_date end) as plan_date_1,
       max(case when seqnum = 2 then plan_date end) as plan_date_2
from (select t.*,
             row_number() over (partition by child_code order by plan_date) as seqnum
      from t
     ) t
group by child_code;

如果您知道所需的最大计划日期数,则只能使用此方法。否则,您将需要使用动态枢轴。思路是一样的,但是需要构造查询字符串,然后传递给sp_executesql

编辑:

如果你只有两个值,那么group by 可能更容易。下面处理只有一个值的情况:

select child_code, min(plan_date) as plan_date_1,
       (case when min(plan_date) <> max(plan_date) then max(plan_date)
        end) as plan_date_2
from t
group by child_code;

【讨论】:

  • 哇!!有用!!没问题! plan_date 仅限于两个!!亲爱的戈登林诺夫,我需要你的更多帮助!!等等我会在这里提到你..希望你能帮助我
【解决方案2】:

如果plan_date 的最大数量未知,您将需要使用动态sql。您将需要使用row_number() 对由Child_Code 分区的每个列表进行编号,以便与pivot() 一起使用。

测试设置:

create table t (child_code varchar(6), plan_date datetime);
insert into t values ('000001','20170221'),('000001','20170321');
declare @cols nvarchar(max);
declare @sql  nvarchar(max);

  select @cols = stuff((
    select distinct 
      ',' + quotename('Plan_Date_'
          +convert(nvarchar(10),row_number() over (
              partition by Child_Code 
              order by     Plan_Date 
          ))
          )
      from t 
      for xml path (''), type).value('.','nvarchar(max)')
    ,1,1,'');

select @sql = '
 select Child_Code, ' + @cols + '
 from  (
  select 
      Child_Code
    , Plan_Date
    , rn=''Plan_Date_''+convert(nvarchar(10),row_number() over (
          partition by Child_Code 
          order by     Plan_Date 
        ))
  from t
    ) as a
 pivot (max([Plan_Date]) for [rn] in (' + @cols + ') ) p';
 select @sql as CodeGenerated;
 exec sp_executesql @sql;

rextester 演示http://rextester.com/YQCR87525

生成的代码:

 select Child_Code, [Plan_Date_1],[Plan_Date_2]
 from  (
  select 
      Child_Code
    , Plan_Date
    , rn='Plan_Date_'+convert(nvarchar(10),row_number() over (
          partition by Child_Code 
          order by     Plan_Date 
        ))
  from t
    ) as a
 pivot (max([Plan_Date]) for [rn] in ([Plan_Date_1],[Plan_Date_2]) ) p

返回

+------------+---------------------+---------------------+
| Child_Code |     Plan_Date_1     |     Plan_Date_2     |
+------------+---------------------+---------------------+
|     000001 | 21.02.2017 00:00:00 | 21.03.2017 00:00:00 |
+------------+---------------------+---------------------+

【讨论】:

  • 感谢您的回答!!它也适用于我!
  • @TanvirArjel 乐于助人!
  • 请再次查看我的问题..我已对其进行了一些扩展,因为我已将 Plan_Date_1 替换为 SewingStart 并将 Plan_Date_2 替换为 SewingEnd!!
猜你喜欢
  • 2023-01-25
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多