declare @tbl table
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into @tbl
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from @tbl a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup'
exec(@sql)
drop table #tbltmp(10 個資料列受到影響)
原因 01 02 03 合計
-------- ------------ ------------ ------------ ------------
5S 30 0 0 30
休息 120 60 60 240
待料 0 15 0 15
待排程 360 180 0 540
換部品 20 10 0 30
換線 0 15 0 15
百分比 36.81% 19.44% 4.17%
還要新增TimeNum1行和原因的總合,還有百分比
格式如下:
原因 01 02 03 合計
-------- ------------ ------------ ------------ ------------
5S 30 0 0 30
休息 120 60 60 240
待料 0 15 0 15
待排程 360 180 0 540
換部品 20 10 0 30
換線 0 15 0 15
合計 530 280 60 870
TimeNum1 60 60 60 180
百分比(合計/TimeNum1) 36.81% 19.44% 4.17% 600%
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into @tbl
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from @tbl a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup'
exec(@sql)
drop table #tbltmp(10 個資料列受到影響)
原因 01 02 03 合計
-------- ------------ ------------ ------------ ------------
5S 30 0 0 30
休息 120 60 60 240
待料 0 15 0 15
待排程 360 180 0 540
換部品 20 10 0 30
換線 0 15 0 15
百分比 36.81% 19.44% 4.17%
還要新增TimeNum1行和原因的總合,還有百分比
格式如下:
原因 01 02 03 合計
-------- ------------ ------------ ------------ ------------
5S 30 0 0 30
休息 120 60 60 240
待料 0 15 0 15
待排程 360 180 0 540
換部品 20 10 0 30
換線 0 15 0 15
合計 530 280 60 870
TimeNum1 60 60 60 180
百分比(合計/TimeNum1) 36.81% 19.44% 4.17% 600%
解决方案 »
- 使用sp_executesql时出错
- 请问在SQL Server 2005 中是如何知道一条SQL 语句运行多长时间(毫秒,不是右下方的显示多少秒)、 CPU 运行率 等等运行信息
- sql server2000新建组后无法注册,提示:[SQL-DMO]需要使用sql 2005管理工具
- sql2005时间戳变成负数的问题
- 能否求证数据库无损连接分解?
- 存储过程中日期可以为空、那怎么插入数据?
- 数据库连接不上?!
- 关于Sql 2000数据库转化为ACCESS的问题。
- 用什么函数可以实现把某一实型值转换为整形值,在查询分析器中?
- 单机上使用数据库要求查询速度最快,数据表50个以内,单表数据量在50万行以内.
- |zyciis|分页存储过程中,我如何知道我的某条记录在哪一页中 谢谢
- MySQL安装时出错,说是端口的问题
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into temp1
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from temp1 a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup' set @sql=@sql+' union select ''合计'','
select @sql=@sql+'cast((select sum(num) from #tbltmp where rq='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) b
set @sql=@sql+'cast((select sum(num) from #tbltmp) as varchar)'set @sql=@sql+' union select ''TimeNum1'', '
select @sql=@sql+'cast((select top 1 TimeNum1 from temp1 where right(bzrq,2)='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) c
set @sql=@sql+'cast((select sum(TimeNum1) from (select distinct right(bzrq,2) rq,TimeNum1 from temp1) f) as varchar)'
--print @sql
set @sql='select * from ('+@sql+') z order by z.[原因]'
exec(@sql)
drop table #tbltmp ,temp1/*
原因 01 02 03 合計
-------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
5S 30 0 0 30
TimeNum1 60 60 60 180
百分比 36.81% 19.44% 4.17%
待料 0 15 0 15
待排程 360 180 0 540
合计 530 280 60 870
換部品 20 10 0 30
換線 0 15 0 15
休息 120 60 60 240*/
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into temp1
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from temp1 a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup' set @sql=@sql+' union select ''合计'','
select @sql=@sql+'cast((select sum(num) from #tbltmp where rq='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) b
set @sql=@sql+'cast((select sum(num) from #tbltmp) as varchar)'set @sql=@sql+' union select ''TimeNum1'', '
select @sql=@sql+'cast((select top 1 TimeNum1 from temp1 where right(bzrq,2)='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) c
set @sql=@sql+'cast((select sum(TimeNum1) from (select distinct right(bzrq,2) rq,TimeNum1 from temp1) f) as varchar)'
--print @sql
set @sql='select * from ('+@sql+') z order by case z.[原因] when ''合计'' then ''300'' when ''TimeNum1'' then ''200'' when ''百分比'' then ''100'' else z.[原因] end desc '
exec(@sql)
drop table #tbltmp ,temp1/*
原因 01 02 03 合計
-------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
休息 120 60 60 240
換線 0 15 0 15
換部品 20 10 0 30
待排程 360 180 0 540
待料 0 15 0 15
5S 30 0 0 30
合计 530 280 60 870
TimeNum1 60 60 60 180
百分比 36.81% 19.44% 4.17% */
create table temp1
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into temp1
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from temp1 a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup' set @sql=@sql+' union select ''合计'','
select @sql=@sql+'cast((select sum(num) from #tbltmp where rq='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) b
set @sql=@sql+'cast((select sum(num) from #tbltmp) as varchar)'set @sql=@sql+' union select ''TimeNum1'', '
select @sql=@sql+'cast((select top 1 TimeNum1 from temp1 where right(bzrq,2)='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) c
set @sql=@sql+'cast((select sum(TimeNum1) from (select distinct right(bzrq,2) rq,TimeNum1 from temp1) f) as varchar)'
--print @sql
set @sql='select * from ('+@sql+') z order by case z.[原因] when ''合计'' then ''300'' when ''TimeNum1'' then ''200'' when ''百分比'' then ''100'' else z.[原因] end desc '
exec(@sql)
drop table #tbltmp ,temp1/*
原因 01 02 03 合計
-------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
休息 120 60 60 240
換線 0 15 0 15
換部品 20 10 0 30
待排程 360 180 0 540
待料 0 15 0 15
5S 30 0 0 30
合计 530 280 60 870
TimeNum1 60 60 60 180
百分比 36.81% 19.44% 4.17% */
create table temp1
(
bzrq nchar(8),
bzzb nvarchar(8),
bzline nvarchar(8),
bzcplb nvarchar(8),
bztype nvarchar(8),
TimeNum int,
TimeNum1 int)
insert into temp1
select '20080101','半成品A1','line1',' 組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','5S', 30,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080101','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080101','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','待排程',180,60 union all
select '20080102','半成品A1','line1','組立前制','待料',15,60 union all
select '20080103','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換部品',10,60 union all
select '20080102','半成品A1','line1','組立前制','休息',60,60 union all
select '20080102','半成品A1','line1','組立前制','換線',15,60 create table #tbltmp
(
bztype nchar(8),
rq nchar(2),
num int
)
insert into #tbltmp
select a.bztype,a.rq,sum(TimeNum) as num from
(
select bztype,right(bzrq,2)as rq,TimeNum from temp1 a
) as a group by a.bztype,a.rq
declare @sql varchar(8000)
set @sql = ' select isnull(bztype,''百分比'') [原因],'
select @sql= @sql+'case when grouping(bztype)=0 then rtrim(sum(case rq when '''+rq+''' then num else 0 end)) else ltrim(str( sum(case rq when '''+rq+''' then num else 0 end)*100.0/1440,8,2))+''%'' end as ['+rq+'],'
from (select distinct rq from #tbltmp ) a
set @sql = stuff(@sql,len(@sql),1,'')
set @sql=@sql+ ',case when grouping(bztype)=0 then rtrim(sum(num)) else '''' end 合計 from #tbltmp group by bztype with rollup' set @sql=@sql+' union select ''合计'','
select @sql=@sql+'cast((select sum(num) from #tbltmp where rq='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) b
set @sql=@sql+'cast((select sum(num) from #tbltmp) as varchar)'set @sql=@sql+' union select ''TimeNum1'', '
select @sql=@sql+'cast((select top 1 TimeNum1 from temp1 where right(bzrq,2)='''+rq+''') as varchar),' from (select distinct rq from #tbltmp ) c
set @sql=@sql+'cast((select sum(TimeNum1) from (select distinct right(bzrq,2) rq,TimeNum1 from temp1) f) as varchar)'
--print @sql
set @sql='select * from ('+@sql+') z order by case z.[原因] when ''合计'' then ''300'' when ''TimeNum1'' then ''200'' when ''百分比'' then ''100'' else z.[原因] end desc '
exec(@sql)
drop table #tbltmp ,temp1/*
原因 01 02 03 合計
-------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
休息 120 60 60 240
換線 0 15 0 15
換部品 20 10 0 30
待排程 360 180 0 540
待料 0 15 0 15
5S 30 0 0 30
合计 530 280 60 870
TimeNum1 60 60 60 180
百分比 36.81% 19.44% 4.17% */