简体   繁体   English

如何在Sql Server 2008 R2中将列转换为行?

[英]How to Convert Columns to Rows in Sql Server 2008 R2?

I have a table like this 我有一张这样的桌子

在此输入图像描述

and the result should be like this 结果应该是这样的

在此输入图像描述

i am little bit confused about Pivot and unpivot and cross apply. 我对Pivot和unpivot和交叉申请有点困惑。 can anyone help me from this. 任何人都可以帮助我。

You are pivoting on two columns (department, [check/uncheck]) . 您正在两个列(department, [check/uncheck])上进行旋转。 As far as I know, that means you cannot use SQL Server's pivot syntax. 据我所知,这意味着你不能使用SQL Server的pivot语法。

One way is to "unpivot" (aka "normalize") checked in a subquery. 一种方法是在子查询中checked “unpivot”(也称为“规范化”)。 You can then "pivot" (aka "denormalize") the tools column in the outer query: 然后,您可以“旋转”(也称为“非规范化”)外部查询中的tools列:

select  department
,       [Check/Uncheck]
,       sum(case when tools = 'engine' then nr else 0 end) as engine
,       sum(case when tools = 'oils' then nr else 0 end) as oils
,       sum(case when tools = 'grease' then nr else 0 end) as grease
,       sum(case when tools = 'sounds' then nr else 0 end) as sounds
,       sum(case when tools = 'wapers' then nr else 0 end) as wapers
from    (
        select  department
        ,       tools
        ,       'Checked' as [Check/Uncheck]
        ,       checked as nr
        from    dbo.YourTable
        union all
        select  department
        ,       tools
        ,       'Unchecked'
        ,       unchecked
        from    dbo.YourTable
        ) as SubQueryAlias
group by
        Department
,       [Check/Uncheck]
order by
        Department
,       [Check/Uncheck]

Live example at SQL Fiddle. SQL Fiddle的实例。

You can use the PIVOT function to get the result, but first you will want to unpivot the unchecked and checked columns. 您可以使用PIVOT函数来获取结果,但首先您需要unchecked和已checked列。 Since you are using SQL Server 2008r2, you can use CROSS APPLY to UNPIVOT the columns into multiple rows. 由于您使用的是SQL Server 2008r2,因此可以使用CROSS APPLY将列UNPIVOT分成多行。

The basic syntax for CROSS APPLY will be: CROSS APPLY的基本语法是:

select department, tools,
  [check/uncheck],
  value
from yourtable
cross apply
(
  values
    ('checked', checked),
    ('unchecked', unchecked)
) c([check/uncheck], value);

See Demo , this gets your data into the format: 请参阅演示 ,这会将您的数据转换为以下格式:

| DEPARTMENT |  TOOLS | CHECK/UNCHECK | VALUE |
|------------|--------|---------------|-------|
|   Maintain | engine |       checked |     0 |
|   Maintain | engine |     unchecked |     0 |
|   Maintain |   oils |       checked |     0 |
|   Maintain |   oils |     unchecked |     2 |

Once the data has been converted into this format, then you can use the PIVOT function to turn your tools into columns: 将数据转换为此格式后,您可以使用PIVOT函数将tools转换为列:

select department,
  [check/uncheck],
  engine, oils, grease, sounds, wapers
from
(
  select department, tools,
    [check/uncheck],
    value
  from yourtable
  cross apply
  (
    values
      ('checked', checked),
      ('unchecked', unchecked)
  ) c([check/uncheck], value)
) d
pivot
(
  sum(value)
  for tools in (engine, oils, grease, sounds, wapers)
) piv
order by department;

See SQL Fiddle with Demo . 请参阅SQL Fiddle with Demo This will give a result of: 这将得到以下结果:

| DEPARTMENT | CHECK/UNCHECK | ENGINE | OILS | GREASE | SOUNDS | WAPERS |
|------------|---------------|--------|------|--------|--------|--------|
|   Maintain |       checked |      0 |    0 |      5 |      0 |      0 |
|   Maintain |     unchecked |      0 |    2 |      1 |      0 |      0 |
| Operations |       checked |      1 |    2 |      1 |      5 |      0 |
| Operations |     unchecked |      0 |    1 |      2 |      1 |      2 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM