| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3006 人关注过本帖
标题:求大神帮忙解读SQL语句
只看楼主 加入收藏
tan6665139
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-12
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
求大神帮忙解读SQL语句
求告知计算可比水泥数据具体使用的数据或者全文解读

USE [SE_Dashboard]
GO
/****** Object:  StoredProcedure [dbo].[GetDataForBarOrLineChart_ByMonth]    Script Date: 05/12/2016 14:54:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:        <Xiaolin-Merlin.Li>
-- Create date: <2014-08-26>
-- Description:    <时间选择条件为某个月,查询该月下的KPI>
-- =============================================
ALTER PROCEDURE [dbo].[GetDataForBarOrLineChart_ByMonth]
    -- Add the parameters for the stored procedure here
    @plantName        nvarchar(50),
    @workcenterName    nvarchar(4000),
    @kpiName        nvarchar(200),
    @selectedMonth    datetime,
    @filters        nvarchar(4000)
   
AS
BEGIN
   
    insert into dbo.log(kpiname,message)values(@kpiName,CONVERT(nvarchar(120),@selectedMonth)+@filters)   
    declare @amplaFilters nvarchar(4000)
    create Table #t2
    (
        first_starttime datetime,
        first_endtime datetime,
        first_amplaFilters nvarchar(20),
        second_starttime datetime,
        sencond_endtime datetime,
        second_amplaFilters nvarchar(20)
    )
    insert into #t2 exec dbo.ConvertTime_Filter_ByMonth
    @plantName,@selectedMonth,@filters
   
    declare @first_starttime datetime
    declare @first_endtime datetime
    declare @second_starttime datetime
    declare @second_endtime datetime
   
    declare @first_amplaFilters nvarchar(20)
    declare @second_amplaFilters nvarchar(20)
   
    select
    @first_starttime=first_starttime,
    @first_endtime = first_endtime,
    @first_amplaFilters=first_amplaFilters,
    @second_starttime=second_starttime,
    @second_endtime =sencond_endtime,
    @second_amplaFilters =second_amplaFilters
    from #t2
   
   
    declare @username nvarchar(100)
    declare @password nvarchar(100)
    declare @servername nvarchar(100)
    declare @dateOff datetime
   
    select @username=UserName,@password=[Password],@servername=ServerName,@dateOff=WorkDayStartTime from T_AmplaServerInfo where PlantName=@plantName
   
    declare @location nvarchar(4000)
    declare @fields nvarchar(4000)
    declare @datasrc nvarchar(200)
    declare @nonemetricsfilters    nvarchar(4000)
    declare @spname nvarchar(2000)
    declare @viewName nvarchar(200)
   
   
    select @location=AmplaLocation,@fields=AmplaFields,@datasrc=DataSrc,@nonemetricsfilters=AmplaFilters,@spname=SPName
        from T_KPIsLocation where PlantName=@plantName and WorkcenterName=@workcenterName and KPIName=@kpiName
   
    set @viewName = ''
   
    --  insert into dbo.log(kpiname,message)values(@kpiName,@datasrc)   
    create table #t1 (
        日期    datetime,
                float
    )
   
    if @plantName <> '默认'   
    begin
        if @datasrc <> 'SP'
        begin            
            if @datasrc = 'Metrics'
            begin
                if(@filters='月' or @first_starttime>@first_endtime)
                begin
                    insert into #t1 exec GetDataByLocationV200806
                    'Metrics',
                    @location,
                    '',
                    @first_starttime,
                    @second_endtime,
                    @viewName,
                    @fields,
                    '',
                    @second_amplaFilters,
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
                end
                else
                begin
                    insert into #t1 exec GetDataByLocationV200806
                    'Metrics',
                    @location,
                    '',
                    @first_starttime,
                    @first_endtime,
                    @viewName,
                    @fields,
                    '',
                    @first_amplaFilters,
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
        
                    insert into #t1 exec GetDataByLocationV200806
                    'Metrics',
                    @location,
                    '',
                    @second_starttime,
                    @second_endtime,
                    @viewName,
                    @fields,
                    '',
                    @second_amplaFilters,
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
                end   
            end
            else if @datasrc = '可比熟料综合能耗' or @datasrc = '可比熟料综合煤耗' or @datasrc = '可比熟料综合电耗'
             or @datasrc = '可比水泥综合电耗' or @datasrc = '可比水泥综合能耗'
            begin
                create Table #t3
                (
                SampleDate datetime,
                DH float,
                MH float,
                KBDH float,
                KBMH float,
                KBNH float
                )
                declare @DateValue datetime
                set @DateValue=getdate()
                IF  EXISTS (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#Quality') and type='U')
                Drop table #Quality
                create Table #Quality (SampleDate  Date, QualityValue  float)   
                IF  EXISTS (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#Production') and type='U')
                Drop table #Production
                create Table #Production (SampleDate  Date, ProductionValue  float)   
                IF  EXISTS (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#KBKPI') and type='U')
                Drop table #KBKPI
                create Table #KBKPI (SampleDate  Date, DH  float,MH  float)
               
               
            

                declare @XZ float
                declare @StartDate date
                declare @EndDate date
                declare @StartDateQly date
                declare @EndDateQly date
                declare @day int

                set @day =DAY(@DateValue)
                print (str(year(Dateadd(MONTH,-1,@DateValue))) + '-' + str(month(Dateadd(MONTH,-1,@DateValue))) + '-01')
               
                set @StartDate=Convert(date,str(year(Dateadd(MONTH,0,@DateValue))) + '-' + str(month(Dateadd(MONTH,0,@DateValue))) + '-01')+@dateOff
                set @EndDate=@DateValue--str(year(@DateValue)) + '-' +STR( month(@DateValue)) + '-01'
                --if (@day>=28)
                --begin
                --set @StartDateQly=@StartDate  --=Convert(date,str(year(Dateadd(MONTH,0,@DateValue))) + '-' + str(month(Dateadd(MONTH,0,@DateValue))) + '-01')+@dateOff
                --set @EndDateQly=@EndDate      --=str(year(@DateValue)) + '-' +STR( month(@DateValue)) + '-01'
 
                --end
                --else
                begin
                set @StartDateQly=Dateadd(MONTH,-1,@StartDate)--Convert(Date,str(year(Dateadd(MONTH,-1,@DateValue))) + '-' + str(month(Dateadd(MONTH,-1,@DateValue))) + '-01')+@dateOff
                set @EndDateQly=@StartDate --str(year(Dateadd(MONTH,-1,@DateValue))) + '-' + str(month(Dateadd(MONTH,-2,@DateValue))) + '-01'+@dateOff

                end

                --    select @StartDate as a,@EndDate as b
           
                set @location='奎山冀东.'+@plantName+'.熟料烧成.窑.质量信息'
                insert into  #Quality exec GetDataByLocationV200806
                'Quality',
                @location,--'奎山冀东.临城一厂.熟料烧成.窑.质量信息',
                '',
                @StartDateQly,
                @EndDateQly,
                '',
                'Sample Period,检验结果',
                '',
                '检验项目={28天抗压强度}',
                1,
                '',
                @username,
                @password,
                @servername,
                1,
                1
            
                set @location='奎山冀东.'+@plantName+'.熟料烧成.窑.绩效信息'
                insert into  #Production exec GetDataByLocationV200806
                'Metrics',
                @location,--'奎山冀东.临城一厂.熟料烧成.窑.绩效信息',
                '',
                @StartDateQly,
                @EndDateQly,
                '',
                'Start Time,产量',
                '',
                'Period={工作日.*}',
                1,
                '',
                @username,
                @password,
                @servername,
                1,
                1
               
                --采用当前月的值
                select   @XZ =POWER(( 52.5/(sum(QualityValue * ProductionValue )/SUM(ProductionValue))),0.25)  from  #Quality     a inner join #Production b on a.SampleDate=b.SampleDate and a.QualityValue>0 and b.ProductionValue>0
            
                set @location='奎山冀东.'+@plantName+'.厂级信息.绩效信息'
                insert into  #KBKPI exec GetDataByLocationV200806
                'Metrics',
                @location,--'奎山冀东.临城一厂.厂级信息.绩效信息',
                '',
                @selectedMonth,
                @selectedMonth,
                '',
                'Start Time,熟料综合单位电耗,熟料综合单位标煤耗减余热',
                '',
                --'Period={工作月.*}',
                '@GroupBy={工作月}',
                1,
                '',
                @username,
                @password,
                @servername,
                1,
                1
                --select * from #KBKPI
                --Select SampleDate,DH,MH, DH * @XZ as KBDH,MH * @XZ as KBMH, 0.1229* DH * @XZ + MH *@XZ as KBNH from #KBKPI   
                declare @kbnh float
                set @kbnh=0
                declare @kbmh float
                set @kbmh=0
                Select  @kbmh=MH * @XZ from #KBKPI   
               
                if @datasrc = '可比熟料综合煤耗'
                begin
                    Select  @kbnh=MH * @XZ from #KBKPI   
                    insert into #t1 values(getdate(),@kbnh)
                end
                else if @datasrc = '可比熟料综合能耗'
                begin
                    Select  @kbnh=0.1229* DH *@XZ + MH *@XZ from #KBKPI   
                    insert into #t1 values(getdate(),@kbnh)
                end
                else if @datasrc = '可比熟料综合电耗'
                begin
                    Select  @kbnh=DH*@XZ from #KBKPI   
                    insert into #t1 values(getdate(),@kbnh)
                end
                if(@plantName='隆尧三厂' and (@datasrc = '可比水泥综合电耗' or @datasrc = '可比水泥综合能耗') )
                begin
                    set @location='奎山冀东.'+@plantName+'.水泥粉磨.1号水泥磨.绩效信息'
                    delete from #Production
                    insert into  #Production exec GetDataByLocationV200806
                    'Metrics',
                    @location,--'奎山冀东.临城一厂.熟料烧成.窑.绩效信息',
                    '',
                    @StartDateQly,
                    @EndDateQly,
                    '',
                    'Start Time,产量',
                    '',
                    'Period={工作日.*}',
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
               
                   set @location='奎山冀东.'+@plantName+'.水泥粉磨.1号水泥磨.质量信息'
                   delete from #Quality
                    insert into  #Quality exec GetDataByLocationV200806
                    'Quality',
                    @location,--'奎山冀东.临城一厂.熟料烧成.窑.质量信息',
                    '',
                    @StartDateQly,
                    @EndDateQly,
                    '',
                    'Sample Period,检验结果',
                    '',
                    '检验项目={28天抗压强度}',
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
                    select   @XZ =POWER(( 42.5/(sum(QualityValue * ProductionValue )/SUM(ProductionValue))),0.25)  
                    from  #Quality     a inner join #Production b on a.SampleDate=b.SampleDate
                    and a.QualityValue>0 and b.ProductionValue>0
                    
                    delete From #Production
                    set @location='奎山冀东.'+@plantName+'.厂级信息.绩效信息'
                    insert into  #Production exec GetDataByLocationV200806
                    'Metrics',
                    @location,--'奎山冀东.临城一厂.厂级信息.绩效信息',
                    '',
                    @selectedMonth,
                    @selectedMonth,
                    '',
                    'Start Time,水泥综合单位电耗',
                    '',
                    --'Period={工作月.*}',
                    '@GroupBy={工作月}',
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
                    Select  @kbnh=ProductionValue * @XZ from #Production   
                    if @datasrc = '可比水泥综合电耗'
                    begin
                        insert into #t1 values(getdate(),@kbnh)
                    end
                    else if (@datasrc='可比水泥综合能耗')
                    begin
                        delete From #Production
                        set @location='奎山冀东.'+@plantName+'.水泥粉磨.绩效信息'
                        insert into  #Production exec GetDataByLocationV200806
                        'Metrics',
                        @location,--'奎山冀东.临城一厂.厂级信息.绩效信息',
                        '',
                        @selectedMonth,
                        @selectedMonth,
                        '',
                        'Start Time,熟料水泥比',
                        '',
                        --'Period={工作月.*}',
                        '@GroupBy={工作月}',
                        1,
                        '',
                        @username,
                        @password,
                        @servername,
                        1,
                        1
                        declare @bi float
                        select @bi=ProductionValue from #Production
                        insert into #t1 values(getdate(),@kbmh*@bi+0.1229*@kbnh)
                    end
                    select * from #t1 order by 日期 desc
                end
                else
                begin
                    select * from #t1 order by 日期 desc
                end
            --insert into dbo.log(kpiname,message)values(@datasrc,@kbnh)
            
            --insert into dbo.log(kpiname,message)values(@datasrc,'ww')
        
            end
            --非绩效管理点
            else
            begin
            insert into dbo.log_d(plantname,workcentername,kpiname,message)values(@plantname,@workcenterName,@kpiName,@second_amplaFilters)   
                set @second_amplaFilters=@nonemetricsfilters
                insert into #t1 exec GetDataByLocationV200806
                    @datasrc,
                    @location,
                    '',
                    @first_starttime,
                    @second_endtime,
                    @viewName,
                    @fields,
                    '',
                    @second_amplaFilters,
                    1,
                    '',
                    @username,
                    @password,
                    @servername,
                    1,
                    1
            end
        
            --insert into t1 select * from #t1
            select * from #t1 order by 日期 desc
            
        end   
        else
        begin
            -- SP 处理逻辑
            declare @strSQL nvarchar(4000)
            
            --set @strSQL = 'insert into #t1 exec '+@spname+' '''+@amplaSamplePeriod+''','''+Convert(nvarchar(20),@startDateLocalTime,120)+''','''+Convert(nvarchar(20),@endDateLocalTime,120)+''''
            
            set @strSQL = 'exec '+@spname+' '''+Convert(nvarchar(20),@first_starttime,120)+''','''+Convert(nvarchar(20),@second_endtime,120)+''''
            
            --print @strSQL;
            
            exec (@strSQL);
            
        end
            
    end
    else
    begin
        select * from #t1 order by 日期 desc
    end
   

   

   
   
   
   
END



搜索更多相关主题的帖子: Object Create 
2016-05-12 15:01
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:191
帖 子:3147
专家分:8408
注 册:2015-3-25
收藏
得分:14 
水平有限,只知道是 1个存储过程,查询。
2016-05-12 16:42
tan6665139
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-12
收藏
得分:0 
回复 2楼 mywisdom88
那也谢谢了
2016-05-12 16:52
快速回复:求大神帮忙解读SQL语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051693 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved