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. can anyone help me from this.
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. can anyone help me from this.
您在两列上旋转(department, [check/uncheck])
。据我所知,这意味着您不能使用 SQL Server 的pivot
语法。
一种方法是在子查询中“取消透视”(又名“规范化”)checked
。然后,您可以在外部查询中“旋转”(也称为“非规范化”)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]
您可以使用 PIVOT 函数来获取结果,但首先您需要取消旋转unchecked
和checked
列。由于您使用的是 SQL Server 2008r2,因此您可以使用 CROSS APPLY 将列取消透视为多行。
CROSS APPLY 的基本语法是:
select department, tools,
[check/uncheck],
value
from yourtable
cross apply
(
values
('checked', checked),
('unchecked', unchecked)
) c([check/uncheck], value);
请参阅Demo,这会将您的数据转换为以下格式:
| DEPARTMENT | TOOLS | CHECK/UNCHECK | VALUE |
|------------|--------|---------------|-------|
| Maintain | engine | checked | 0 |
| Maintain | engine | unchecked | 0 |
| Maintain | oils | checked | 0 |
| Maintain | oils | unchecked | 2 |
将数据转换为这种格式后,您可以使用 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;
请参阅SQL Fiddle with Demo。这将给出以下结果:
| 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 |