/*查询指定机床的基本状态*/
CREATE            procedure Report_MaTolStatus_Group
@ID nvarchar(50),
@Name nvarchar(50),
@begin DateTime,
@end DateTime
as
DECLARE @TimeValue BigInt
SET @TimeValue=DATEDIFF (second,@begin,@end)
begin
if(@Name='All')
begin
        SELECT [status]=case
when grouping(MaTolName)=1 then '合计'
when grouping(status)=1 then '关机'
when grouping(status)=0  then status
end ,statusTimeValue=case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end,dbo.GetTimeStr(case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end)AS STRstatusTimeValue,[Group]=[MaTolName],[TimeValue]=@TimeValue
FROM [dbo].[StatusTable]
WHERE [StartTime] BETWEEN @begin AND @end GROUP BY [MaTolName] ,[status] WITH ROLLUP
end
if(@Name='Dept')
begin
SELECT [status]=case
when grouping(MaTolName)=1 then '合计'
when grouping(status)=1 then '关机'
when grouping(status)=0  then status
end ,statusTimeValue=case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end,dbo.GetTimeStr(case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end)AS STRstatusTimeValue,[Group]=[MaTolName],[TimeValue]=@TimeValue 
FROM [dbo].[StatusTable]
WHERE [DeptID]=cast(@ID AS uniqueidentifier)
AND [StartTime] BETWEEN @begin AND @end GROUP BY [MaTolName],[status] WITH ROLLUP
end
if(@Name='MaTolGrp')
begin
SELECT [status]=case
when grouping(MaTolName)=1 then '合计'
when grouping(status)=1 then '关机'
when grouping(status)=0  then status
end ,statusTimeValue=case
when grouping(status)=1 then @TimeValue -sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end,dbo.GetTimeStr(case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end)AS STRstatusTimeValue,[Group]=[MaTolName],[TimeValue]=@TimeValue 
FROM [dbo].[StatusTable]
WHERE [MaTolGrpID]=cast(@ID AS uniqueidentifier) AND [StartTime] BETWEEN @begin AND @end GROUP BY [MaTolName],[status] WITH ROLLUP
end
if(@Name='MaTol')
begin
SELECT [status]=case
when grouping(MaTolName)=1 then '合计'
when grouping(status)=1 then '关机'
when grouping(status)=0  then status
end ,statusTimeValue=case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end,dbo.GetTimeStr(case
when grouping(status)=1 then @TimeValue-sum([statusTimeValue])
when grouping(status)=0  then sum([statusTimeValue])
end)AS STRstatusTimeValue,[Group]=[MaTolName],[TimeValue]=@TimeValue 
FROM [dbo].[StatusTable]
WHERE [MaTolName]=@ID AND [StartTime] BETWEEN @begin AND @end GROUP BY [MaTolName],[status] WITH ROLLUP
end
end
--和Report_MaTolStatus相比多一个机床分组
GO
/*查询指定机床的运行效率*/
CREATE              procedure Report_MaTolRunEff
@ID nvarchar(50),
@Name nvarchar(50),
@begin DateTime,
@end DateTime,
@Time nvarchar(10)
as
DECLARE @MaTol nvarchar(100),@s nvarchar(4000),@Group nvarchar(200)
SET @Group='[dbo].[GetTimeGroup]([StartTime],'+QUOTENAME(@Time,'''')+')'
begin
if(@Name='All')
begin
SET @MaTol=''
end
if(@Name='Dept')
begin
SET @MaTol='[DeptID]='+QUOTENAME(@ID,'''')+' AND'
end
if(@Name='MaTolGrp')
begin
SET @MaTol='[MaTolGrpID]='+QUOTENAME(@ID,'''')+' AND'
end
if(@Name='MaTol')
begin
SET @MaTol='[MaTolName]='+QUOTENAME(@ID,'''')+' AND'
end
if(@Time='')
begin
select @Group='DeptName+'+QUOTENAME('.','''')+'+MaTolGrpName+'+QUOTENAME('.','''')+'+MaTolName'
end
select @s='SELECT [MaTolName],[RUNNING]=sum(case status when '+QUOTENAME('运行','''')+' then Value else 0 end),dbo.GetTimeStr(sum(case status when '+QUOTENAME('运行','''')+' then Value else 0 end)) AS STRRUNNING
,[ONLINE]=sum(Value),dbo.GetTimeStr(sum(Value)) AS STRONLINE
,[RUNEFF]=cast(sum(case status when '+QUOTENAME('运行','''')+' then Value else 0 end)*10000/sum(Value) as decimal(10,2))/100
,[Group]
From
(Select [status],sum([statusTimeValue]) AS Value,MaTolName=DeptName+'+QUOTENAME('.','''')+'+MaTolGrpName+'+QUOTENAME('.','''')+'+MaTolName ,[Group]='+@Group+' 
 From [StatusTable] WHERE '+@MaTol+' [StartTime] BETWEEN '+QUOTENAME(@begin,'''') +' AND '+QUOTENAME(@end,'''')+' 
Group by [status],MaTolName,DeptName,MaTolGrpName,'+@Group+'
)a group by [MaTolName],[Group]'
--拼字符串做的--不知道效率高不高
EXECUTE(@s)end
GO

解决方案 »

  1.   


    Select sum(statusTimeValue) AS sONLINE,
           sum(GetWorkTime(StartTime)) as WORKING,
           (DeptName|| QUOTENAME('.', '''') ||MaTolGrpName||QUOTENAME('.', '''') ||MaTolName||) as MaTolName,
           '||vGroup||' as sGroup
      From StatusTable
      

  2.   

    Select sum(statusTimeValue) AS sONLINE,
           sum(GetWorkTime(StartTime)) as WORKING,
           ('||DeptName||' || QUOTENAME('.', '''') ||
           '||MaTolGrpName||' || QUOTENAME('.', '''') || '||MaTolName||') as MaTolName,
           '||vGroup||' as sGroup
      From StatusTable