27,579
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE [dbo].[#tmp_aa](
[ftype] varchar(255),
[fdate] varchar(255),
[FHeadSelfB0436] [varchar](255) NULL,
[fqty] [decimal](28, 10) NULL
)
insert into #tmp_aa
select '领料','2018-08-02','CMT-Harness-PM-Line01',156.0500000000 union all
select '领料','2018-08-02','CMT-Harness-PM-Line01',134.0500000000 union all
select '领料','2018-08-03','CMT-Harness-PM-Line01',100 union all
select '领料','2018-08-04','CMT-Harness-PM-Line01',100 union all
select '领料','2018-08-05','CMT-Harness-PM-Line01',100 union all
select '报废','2018-08-02','CMT-Harness-PM-Line01',50 union all
select '报废','2018-08-02','CMT-Harness-PM-Line01',50 union all
select '报废','2018-08-04','CMT-Harness-PM-Line01',100 union all
select '报废','2018-08-05','CMT-Harness-PM-Line01',100 union all
select '领料','2018-08-02','CMT-Harness-PM-Line02',156.0500000000 union all
select '领料','2018-08-02','CMT-Harness-PM-Line02',134.0500000000 union all
select '领料','2018-08-03','CMT-Harness-PM-Line02',100 union all
select '领料','2018-08-04','CMT-Harness-PM-Line02',100 union all
select '领料','2018-08-05','CMT-Harness-PM-Line02',100 union all
select '报废','2018-08-02','CMT-Harness-PM-Line02',50 union all
select '报废','2018-08-02','CMT-Harness-PM-Line02',50 union all
select '报废','2018-08-04','CMT-Harness-PM-Line02',100 union all
select '报废','2018-08-05','CMT-Harness-PM-Line02',100
DECLARE @s NVARCHAR(4000)
SELECT @s = ISNULL(@s + ',', '') + QUOTENAME(fdate)
FROM (select distinct fdate from #tmp_aa ) as A order by fdate ---列名不要重复
Declare @sql NVARCHAR(4000)
SET @sql='
select r.* from
(select FHeadSelfB0436,ftype,fqty,fdate from #tmp_aa) as t
pivot
(
sum(t.fqty)
for t.fdate in ('+@s+')
) as r order by FHeadSelfB0436,ftype '
EXEC( @sql)