表的转置
表的转置
use SSIS
drop table PTV
go
create table PTV
(
TimeID nvarchar(10),
ResourceID int,
KPI1 numeric(38,2),
KPI2 numeric(38,2),
KPI3 numeric(38,2),
KPI4 numeric(38,2),
KPI5 numeric(38,2)
)
go
insert into ptv values('101',1,2.78,3,5,4,4)
insert into ptv values('102',2,2.7,50,95,44,45)
insert into ptv values('103',3,4.99,2,5,4,0)
go
select * from dbo.PTV
go
select TimeID,ResourceID,KPIName,KPIValue
from
( select TimeID,ResourceID,KPI1,KPI2,KPI3,KPI4,KPI5
from PTV
)as p
unpivot
(
KPIValue for KPIName IN
(KPI1,KPI2,KPI3,KPI4,KPI5)
) as unpvt
go
select TimeID,ResourceID,KPIName,KPIValue
from
(
select TimeID,ResourceID,'KPI1' as KPIName,KPI1 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI2' as KPIName,KPI2 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI3' as KPIName,KPI3 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI4' as KPIName,KPI4 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI5' as KPIName,KPI5 as KPIValue
from PTV
) as a
order by TimeID,ResourceID,KPIName
drop table PTV
go
create table PTV
(
TimeID nvarchar(10),
ResourceID int,
KPI1 numeric(38,2),
KPI2 numeric(38,2),
KPI3 numeric(38,2),
KPI4 numeric(38,2),
KPI5 numeric(38,2)
)
go
insert into ptv values('101',1,2.78,3,5,4,4)
insert into ptv values('102',2,2.7,50,95,44,45)
insert into ptv values('103',3,4.99,2,5,4,0)
go
select * from dbo.PTV
go
select TimeID,ResourceID,KPIName,KPIValue
from
( select TimeID,ResourceID,KPI1,KPI2,KPI3,KPI4,KPI5
from PTV
)as p
unpivot
(
KPIValue for KPIName IN
(KPI1,KPI2,KPI3,KPI4,KPI5)
) as unpvt
go
select TimeID,ResourceID,KPIName,KPIValue
from
(
select TimeID,ResourceID,'KPI1' as KPIName,KPI1 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI2' as KPIName,KPI2 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI3' as KPIName,KPI3 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI4' as KPIName,KPI4 as KPIValue
from PTV
union all
select TimeID,ResourceID,'KPI5' as KPIName,KPI5 as KPIValue
from PTV
) as a
order by TimeID,ResourceID,KPIName