SQL server 行列转换

DECLARE @sql VARCHAR(8000)


SET @sql=''  --初始化变量@sql


SELECT @sql=@sql+','+dates FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值


SET @sql=stuff(@sql,1,1,'')--去掉首个','


SET @sql='select ids, '+@sql+' from InOutDoor3 pivot (max(firstintime) for dates in ('+@sql+'))a'


exec(@sql)






--firstin
DECLARE @sql VARCHAR(8000)


DECLARE @firstin VARCHAR(8000) 


SET @sql=''  --初始化变量@sql
SET @firstin=''  --初始化变量@firstin


SELECT @sql=@sql+','+dates FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstin=@firstin+','+dates+' as '+dates+'firstin' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值


SET @sql=stuff(@sql,1,1,'')--去掉首个','
SET @firstin=stuff(@firstin,1,1,'')--去掉首个','


SET @sql='select ids, '+@firstin+' from InOutDoor3 pivot (max(firstintime) for dates in ('+@sql+'))a'


exec(@sql)


--fitstout
DECLARE @sql VARCHAR(8000)


DECLARE @firstout VARCHAR(8000) 


SET @sql=''  --初始化变量@sql
SET @firstout=''  --初始化变量@firstout


SELECT @sql=@sql+','+dates FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstout=@firstout+','+dates+' as '+dates+'firstout' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值


SET @sql=stuff(@sql,1,1,'')--去掉首个','
SET @firstout=stuff(@firstout,1,1,'')--去掉首个','


SET @sql='select ids, '+@firstout+' from InOutDoor3 pivot (max(firstouttime) for dates in ('+@sql+'))a'


exec(@sql)




firstinout


DECLARE @sql VARCHAR(8000)
DECLARE @firstin VARCHAR(8000) 
DECLARE @sqlfirstin VARCHAR(8000) 
DECLARE @firstout VARCHAR(8000)
DECLARE @sqlfirstout VARCHAR(8000) 
DECLARE @allinout VARCHAR(8000)
declare @sqlallinout varchar(8000)


SET @sql=''  --初始化变量@sql
SET @firstin=''  --初始化变量@firstin
set @sqlfirstin=''  --初始化变量@sqlfirstin
SET @firstout=''  --初始化变量@firstout
set @sqlfirstout=''  --初始化变量@sqlfirstout
set @allinout=''  --初始化变量@allinout
set @sqlallinout=''


SELECT @sql=@sql+','+dates FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstin=@firstin+',max('+dates+') as '+dates+'firstin' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstout=@firstout+',max('+dates+') as '+dates+'firstout' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
select @allinout=@allinout+',fin.'+dates+'firstin, fout.'+dates+'firstout' FROM InOutDoor3 GROUP BY dates
order by dates






SET @sql=stuff(@sql,1,1,'')--去掉首个','
SET @firstin=stuff(@firstin,1,1,'')--去掉首个','
SET @firstout=stuff(@firstout,1,1,'')--去掉首个','
set @allinout=stuff(@allinout,1,1,'')--去掉首个','


SET @sqlfirstin='select ids, '+@firstin+' from InOutDoor3 pivot (max(firstintime) for dates in ('+@sql+'))a
group by ids'
SET @sqlfirstout='select ids, '+@firstout+' from InOutDoor3 pivot (max(firstouttime) for dates in ('+@sql+'))a
group by ids'


set @sqlallinout = 'select fin.ids, '+@allinout+' from ('+@sqlfirstin+') fin
left join ('+@sqlfirstout+') fout on  fin.ids=fout.ids order by fin.ids'


print @sqlallinout


exec(@sqlallinout)




allinout


DECLARE @sql VARCHAR(8000)
DECLARE @firstin VARCHAR(8000) 
DECLARE @sqlfirstin VARCHAR(8000) 
DECLARE @firstout VARCHAR(8000)
DECLARE @sqlfirstout VARCHAR(8000)
DECLARE @allinout VARCHAR(8000)
DECLARE @lastin VARCHAR(8000) 
DECLARE @sqllastin VARCHAR(8000) 
DECLARE @lastout VARCHAR(8000)
DECLARE @sqllastout VARCHAR(8000)
declare @sqlallinout varchar(8000)


SET @sql=''  --初始化变量@sql
SET @firstin=''  --初始化变量@firstin
set @sqlfirstin=''  --初始化变量@sqlfirstin
SET @firstout=''  --初始化变量@firstout
set @sqlfirstout=''  --初始化变量@sqlfirstout
SET @lastin=''  --初始化变量@lastin
set @sqllastin=''  --初始化变量@sqllastin
SET @lastout=''  --初始化变量@lastout
set @sqllastout=''  --初始化变量@sqllastout
set @allinout=''  --初始化变量@allinout
set @sqlallinout=''


SELECT @sql=@sql+','+dates FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstin=@firstin+',max('+dates+') as '+dates+'firstin' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @firstout=@firstout+',max('+dates+') as '+dates+'firstout' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @lastin=@lastin+',max('+dates+') as '+dates+'lastin' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
SELECT @lastout=@lastout+',max('+dates+') as '+dates+'lastout' FROM InOutDoor3 GROUP BY dates
order by dates --变量多值赋值
select @allinout=@allinout+',fin.'+dates+'firstin, fout.'+dates+'firstout'+',lin.'+dates+'lastin, lout.'+dates+'lastout' FROM InOutDoor3 GROUP BY dates
order by dates






SET @sql=stuff(@sql,1,1,'')--去掉首个','
SET @firstin=stuff(@firstin,1,1,'')--去掉首个','
SET @firstout=stuff(@firstout,1,1,'')--去掉首个','
SET @lastin=stuff(@lastin,1,1,'')--去掉首个','
SET @lastout=stuff(@lastout,1,1,'')--去掉首个','
set @allinout=stuff(@allinout,1,1,'')--去掉首个','


SET @sqlfirstin='select dept,ids,name, '+@firstin+' from InOutDoor3 pivot (max(firstintime) for dates in ('+@sql+'))a
group by dept,ids,name'
SET @sqlfirstout='select ids, '+@firstout+' from InOutDoor3 pivot (max(firstouttime) for dates in ('+@sql+'))a
group by ids'
SET @sqllastin='select ids, '+@lastin+' from InOutDoor3 pivot (max(lastintime) for dates in ('+@sql+'))a
group by ids'
SET @sqllastout='select ids, '+@lastout+' from InOutDoor3 pivot (max(lastouttime) for dates in ('+@sql+'))a
group by ids'


set @sqlallinout = 'select fin.dept,fin.ids,fin.name, '+@allinout+' from ('+@sqlfirstin+') fin
left join ('+@sqlfirstout+') fout on  fin.ids=fout.ids 
left join ('+@sqllastin+') lin on  fin.ids=lin.ids 
left join ('+@sqllastout+') lout on  fin.ids=lout.ids 
order by fin.dept,fin.ids'


--创建视图
--set @sqlallinout = 'create view li002 as (select fin.dept,fin.ids,fin.name, '+@allinout+' from ('+@sqlfirstin+') fin
--left join ('+@sqlfirstout+') fout on  fin.ids=fout.ids 
--left join ('+@sqllastin+') lin on  fin.ids=lin.ids 
--left join ('+@sqllastout+') lout on  fin.ids=lout.ids)'




exec(@sqlallinout)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值