Dynamic SQL with Dynamic column names

时间:2017-04-24 17:17:11

标签: sql select dynamic

I am trying to build a query that will return 13 columns that will display MTBF for a manufacturing facility. This is for a rolling calendar year by month. I have the formula working but, what I'm having trouble with is when I try to add more then one column and name that column with the 'as' function.

Any pointers are appreciated.

This is what the results should look like.

Results

Here is what I have so far.

Declare @CurrentDate as DateTime

Declare @PreviousMonth as DateTime
Declare @PreviousMonthEnd as DateTime
Declare @PreviousMonthString as VarChar(20)
Declare @PreviousMonthStringEnd as VarChar(20)
Declare @PreviousMonthName as Varchar(20)

Declare @PreviousMonth1 as DateTime
Declare @PreviousMonthEnd1 as DateTime
Declare @PreviousMonthString1 as VarChar(20)
Declare @PreviousMonthStringEnd1 as VarChar(20)
Declare @PreviousMonthName1 as Varchar(20)

Declare @PreviousMonth2 as DateTime
Declare @PreviousMonthEnd2 as DateTime
Declare @PreviousMonthString2 as VarChar(20)
Declare @PreviousMonthStringEnd2 as VarChar(20)
Declare @PreviousMonthName2 as Varchar(20)

Declare @PreviousMonth3 as DateTime
Declare @PreviousMonthEnd3 as DateTime
Declare @PreviousMonthString3 as VarChar(20)
Declare @PreviousMonthStringEnd3 as VarChar(20)
Declare @PreviousMonthName3 as Varchar(20)

Declare @PreviousMonth4 as DateTime
Declare @PreviousMonthEnd4 as DateTime
Declare @PreviousMonthString4 as VarChar(20)
Declare @PreviousMonthStringEnd4 as VarChar(20)
Declare @PreviousMonthName4 as Varchar(20)

Declare @PreviousMonth5 as DateTime
Declare @PreviousMonthEnd5 as DateTime
Declare @PreviousMonthString5 as VarChar(20)
Declare @PreviousMonthStringEnd5 as VarChar(20)
Declare @PreviousMonthName5 as Varchar(20)

Declare @PreviousMonth6 as DateTime
Declare @PreviousMonthEnd6 as DateTime
Declare @PreviousMonthString6 as VarChar(20)
Declare @PreviousMonthStringEnd6 as VarChar(20)
Declare @PreviousMonthName6 as Varchar(20)

Declare @PreviousMonth7 as DateTime
Declare @PreviousMonthEnd7 as DateTime
Declare @PreviousMonthString7 as VarChar(20)
Declare @PreviousMonthStringEnd7 as VarChar(20)
Declare @PreviousMonthName7 as Varchar(20)

Declare @PreviousMonth8 as DateTime
Declare @PreviousMonthEnd8 as DateTime
Declare @PreviousMonthString8 as VarChar(20)
Declare @PreviousMonthStringEnd8 as VarChar(20)
Declare @PreviousMonthName8 as Varchar(20)

Declare @PreviousMonth9 as DateTime
Declare @PreviousMonthEnd9 as DateTime
Declare @PreviousMonthString9 as VarChar(20)
Declare @PreviousMonthStringEnd9 as VarChar(20)
Declare @PreviousMonthName9 as Varchar(20)

Declare @PreviousMonth10 as DateTime
Declare @PreviousMonthEnd10 as DateTime
Declare @PreviousMonthString10 as VarChar(20)
Declare @PreviousMonthStringEnd10 as VarChar(20)
Declare @PreviousMonthName10 as Varchar(20)

Declare @PreviousMonth11 as DateTime
Declare @PreviousMonthEnd11 as DateTime
Declare @PreviousMonthString11 as VarChar(20)
Declare @PreviousMonthStringEnd11 as VarChar(20)
Declare @PreviousMonthName11 as Varchar(20)

Declare @PreviousMonth12 as DateTime
Declare @PreviousMonthEnd12 as DateTime
Declare @PreviousMonthString12 as VarChar(Max)
Declare @PreviousMonthStringEnd12 as VarChar(Max)
Declare @PreviousMonthName12 as Varchar(Max)

Declare @SQLString as Varchar(Max)

IF (DatePart(mm,getdate()) > 1)
        SET @PreviousMonth = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-1,getdate())),01)
    Else
        SET @PreviousMonth = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-1,getdate())),01)

IF (DatePart(mm,getdate()) > 2)
        SET @PreviousMonth1 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-2,getdate())),01)
    Else
        SET @PreviousMonth1 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-2,getdate())),01)

IF (DatePart(mm,getdate()) > 3)
        SET @PreviousMonth2 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-3,getdate())),01)
    Else
        SET @PreviousMonth2 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-3,getdate())),01)

IF (DatePart(mm,getdate()) > 4)
        SET @PreviousMonth3 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-4,getdate())),01)
    Else
        SET @PreviousMonth3 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-4,getdate())),01)

IF (DatePart(mm,getdate()) > 5)
        SET @PreviousMonth4 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-5,getdate())),01)
    Else
        SET @PreviousMonth4 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-5,getdate())),01)

IF (DatePart(mm,getdate()) > 6)
        SET @PreviousMonth5 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-6,getdate())),01)
    Else
        SET @PreviousMonth5 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-6,getdate())),01)

IF (DatePart(mm,getdate()) > 7)
        SET @PreviousMonth6 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-7,getdate())),01)
    Else
        SET @PreviousMonth6 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-7,getdate())),01)

IF (DatePart(mm,getdate()) > 8)
        SET @PreviousMonth7 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-8,getdate())),01)
    Else
        SET @PreviousMonth7 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-8,getdate())),01)

IF (DatePart(mm,getdate()) > 9)
        SET @PreviousMonth8 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-9,getdate())),01)
    Else
        SET @PreviousMonth8 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-9,getdate())),01)

IF (DatePart(mm,getdate()) > 10)
        SET @PreviousMonth9 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-10,getdate())),01)
    Else
        SET @PreviousMonth10 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-10,getdate())),01)

IF (DatePart(mm,getdate()) > 11)
        SET @PreviousMonth11 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-11,getdate())),01)
    Else
        SET @PreviousMonth11 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-11,getdate())),01)

IF (DatePart(mm,getdate()) > 12)
        SET @PreviousMonth12 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-12,getdate())),01)
    Else
        SET @PreviousMonth12 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-12,getdate())),01)

SET @PreviousMonthName = DateName(Month,@PreviousMonth)
SET @PreviousMonthName1 = DateName(Month,@PreviousMonth1)
SET @PreviousMonthName2 = DateName(Month,@PreviousMonth2)
SET @PreviousMonthName3 = DateName(Month,@PreviousMonth3)
SET @PreviousMonthName4 = DateName(Month,@PreviousMonth4)
SET @PreviousMonthName5 = DateName(Month,@PreviousMonth5)
SET @PreviousMonthName6 = DateName(Month,@PreviousMonth6)
SET @PreviousMonthName7 = DateName(Month,@PreviousMonth7)
SET @PreviousMonthName8 = DateName(Month,@PreviousMonth8)
SET @PreviousMonthName9 = DateName(Month,@PreviousMonth9)
SET @PreviousMonthName10 = DateName(Month,@PreviousMonth10)
SET @PreviousMonthName11 = DateName(Month,@PreviousMonth11)
SET @PreviousMonthName12 = DateName(Month,@PreviousMonth12)

SET @PreviousMonthString = Cast(@PreviousMonth as VarChar(20))
SET @PreviousMonthString1 = Cast(@PreviousMonth1 as VarChar(20))
SET @PreviousMonthString2 = Cast(@PreviousMonth2 as VarChar(20))
SET @PreviousMonthString3 = Cast(@PreviousMonth3 as VarChar(20))
SET @PreviousMonthString4 = Cast(@PreviousMonth4 as VarChar(20))
SET @PreviousMonthString5 = Cast(@PreviousMonth5 as VarChar(20))
SET @PreviousMonthString6 = Cast(@PreviousMonth6 as VarChar(20))
SET @PreviousMonthString7 = Cast(@PreviousMonth7 as VarChar(20))
SET @PreviousMonthString8 = Cast(@PreviousMonth8 as VarChar(20))
SET @PreviousMonthString9 = Cast(@PreviousMonth9 as VarChar(20))
SET @PreviousMonthString10 = Cast(@PreviousMonth10 as VarChar(20))
SET @PreviousMonthString11 = Cast(@PreviousMonth11 as VarChar(20))
SET @PreviousMonthString12 =  Cast(@PreviousMonth12 as VarChar(20))

Set @PreviousMonthStringEnd = Cast(Dateadd(Day,-365,@previousMonth) as VarChar(20))
Set @PreviousMonthStringEnd1 = Cast(Dateadd(Day,-365,@previousMonth1) as VarChar(20))
Set @PreviousMonthStringEnd2 = Cast(Dateadd(Day,-365,@previousMonth2) as VarChar(20))
Set @PreviousMonthStringEnd3 = Cast(Dateadd(Day,-365,@previousMonth3) as VarChar(20))
Set @PreviousMonthStringEnd4 = Cast(Dateadd(Day,-365,@previousMonth4) as VarChar(20))
Set @PreviousMonthStringEnd5 = Cast(Dateadd(Day,-365,@previousMonth5) as VarChar(20))
Set @PreviousMonthStringEnd6 = Cast(Dateadd(Day,-365,@previousMonth6) as VarChar(20))
Set @PreviousMonthStringEnd7 = Cast(Dateadd(Day,-365,@previousMonth7) as VarChar(20))
Set @PreviousMonthStringEnd8 = Cast(Dateadd(Day,-365,@previousMonth8) as VarChar(20))
Set @PreviousMonthStringEnd9 = Cast(Dateadd(Day,-365,@previousMonth9) as VarChar(20))
Set @PreviousMonthStringEnd10 = Cast(Dateadd(Day,-365,@previousMonth10) as VarChar(20))
Set @PreviousMonthStringEnd11 = Cast(Dateadd(Day,-365,@previousMonth11) as VarChar(20))
Set @PreviousMonthStringEnd12 = Cast(Dateadd(Day,-365,@previousMonth12) as VarChar(Max))

Declare @Formula as VarChar(Max)
Declare @From as Varchar(Max)
Declare @Where as Varchar(Max)

Set @Formula = 'Cast(Round((COUNT (Distinct wojob.AssetID) *365.0)/(COUNT(*)),2) as Numeric(36,2))'
Set @From = 'FROM [Maint5k].[dbo].[woJob] INNER JOIN Maint5k.dbo.syJobType ON woJob.JobTypeID = syJobType.JobTypeID INNER JOIN Maint5k.dbo.pmAsset ON woJob.AssetID = pmAsset.AssetID'
Set @Where = ' and (pmAsset.Usertext2 = ''Critical'' or pmAsset.Usertext2 = ''Constraint'')) '

Set @SQLString = 'Select (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString12 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd12 + ''' as ''' + @PreviousMonthName12 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString11 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd11 + ''' as ''' + @PreviousMonthName11 + '''),''    
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString10 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd10 + ''' as ''' + @PreviousMonthName10 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString9 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd9 + ''' as ''' + @PreviousMonthName9 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString8 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd8 + ''' as ''' + @PreviousMonthName8 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString7 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd7 + ''' as ''' + @PreviousMonthName7 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString6 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd6 + ''' as ''' + @PreviousMonthName6 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString5 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd5 + ''' as ''' + @PreviousMonthName5 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString4 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd4 + ''' as ''' + @PreviousMonthName4 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString3 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd3 + ''' as ''' + @PreviousMonthName3 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString2 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd2 + ''' as ''' + @PreviousMonthName2 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString1 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd1 + ''' as ''' + @PreviousMonthName1 + '''),''
                         (Select ' + @Formula + ' ' + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd + ''' as ''' + @PreviousMonthName + ''')'



    --print(@SQLString)
    Execute(@SQLString)

Thanks for the Help!

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。如果其他人遇到类似的问题,想要更新帖子。

Declare @CurrentDate as DateTime

Declare @CurrentMonth as DateTime
Declare @CurrentMonthEnd as DateTime
Declare @CurrentMonthString as VarChar(20)
Declare @CurrentMonthStringEnd as VarChar(20)
Declare @CurrentMonthName as Varchar(20)


Declare @PreviousMonth as DateTime
Declare @PreviousMonthEnd as DateTime
Declare @PreviousMonthString as VarChar(20)
Declare @PreviousMonthStringEnd as VarChar(20)
Declare @PreviousMonthName as Varchar(20)

Declare @PreviousMonth1 as DateTime
Declare @PreviousMonthEnd1 as DateTime
Declare @PreviousMonthString1 as VarChar(20)
Declare @PreviousMonthStringEnd1 as VarChar(20)
Declare @PreviousMonthName1 as Varchar(20)

Declare @PreviousMonth2 as DateTime
Declare @PreviousMonthEnd2 as DateTime
Declare @PreviousMonthString2 as VarChar(20)
Declare @PreviousMonthStringEnd2 as VarChar(20)
Declare @PreviousMonthName2 as Varchar(20)

Declare @PreviousMonth3 as DateTime
Declare @PreviousMonthEnd3 as DateTime
Declare @PreviousMonthString3 as VarChar(20)
Declare @PreviousMonthStringEnd3 as VarChar(20)
Declare @PreviousMonthName3 as Varchar(20)

Declare @PreviousMonth4 as DateTime
Declare @PreviousMonthEnd4 as DateTime
Declare @PreviousMonthString4 as VarChar(20)
Declare @PreviousMonthStringEnd4 as VarChar(20)
Declare @PreviousMonthName4 as Varchar(20)

Declare @PreviousMonth5 as DateTime
Declare @PreviousMonthEnd5 as DateTime
Declare @PreviousMonthString5 as VarChar(20)
Declare @PreviousMonthStringEnd5 as VarChar(20)
Declare @PreviousMonthName5 as Varchar(20)

Declare @PreviousMonth6 as DateTime
Declare @PreviousMonthEnd6 as DateTime
Declare @PreviousMonthString6 as VarChar(20)
Declare @PreviousMonthStringEnd6 as VarChar(20)
Declare @PreviousMonthName6 as Varchar(20)

Declare @PreviousMonth7 as DateTime
Declare @PreviousMonthEnd7 as DateTime
Declare @PreviousMonthString7 as VarChar(20)
Declare @PreviousMonthStringEnd7 as VarChar(20)
Declare @PreviousMonthName7 as Varchar(20)

Declare @PreviousMonth8 as DateTime
Declare @PreviousMonthEnd8 as DateTime
Declare @PreviousMonthString8 as VarChar(20)
Declare @PreviousMonthStringEnd8 as VarChar(20)
Declare @PreviousMonthName8 as Varchar(20)

Declare @PreviousMonth9 as DateTime
Declare @PreviousMonthEnd9 as DateTime
Declare @PreviousMonthString9 as VarChar(20)
Declare @PreviousMonthStringEnd9 as VarChar(20)
Declare @PreviousMonthName9 as Varchar(20)

Declare @PreviousMonth10 as DateTime
Declare @PreviousMonthEnd10 as DateTime
Declare @PreviousMonthString10 as VarChar(20)
Declare @PreviousMonthStringEnd10 as VarChar(20)
Declare @PreviousMonthName10 as Varchar(20)

Declare @PreviousMonth11 as DateTime
Declare @PreviousMonthEnd11 as DateTime
Declare @PreviousMonthString11 as VarChar(20)
Declare @PreviousMonthStringEnd11 as VarChar(20)
Declare @PreviousMonthName11 as Varchar(20)

Declare @PreviousMonth12 as DateTime
Declare @PreviousMonthEnd12 as DateTime
Declare @PreviousMonthString12 as VarChar(20)
Declare @PreviousMonthStringEnd12 as VarChar(20)
Declare @PreviousMonthName12 as Varchar(20)

Declare @SQLString as Varchar(Max)
Declare @SQLString1 as Varchar(Max)
Declare @SQLString2 as Varchar(Max)
Declare @SQLString3 as Varchar(Max)

IF (DatePart(mm,getdate()) > 1)
        SET @PreviousMonth = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-1,getdate())),01)
    Else
        SET @PreviousMonth = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-1,getdate())),01)

IF (DatePart(mm,getdate()) > 2)
        SET @PreviousMonth1 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-2,getdate())),01)
    Else
        SET @PreviousMonth1 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-2,getdate())),01)

IF (DatePart(mm,getdate()) > 3)
        SET @PreviousMonth2 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-3,getdate())),01)
    Else
        SET @PreviousMonth2 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-3,getdate())),01)

IF (DatePart(mm,getdate()) > 4)
        SET @PreviousMonth3 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-4,getdate())),01)
    Else
        SET @PreviousMonth3 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-4,getdate())),01)

IF (DatePart(mm,getdate()) > 5)
        SET @PreviousMonth4 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-5,getdate())),01)
    Else
        SET @PreviousMonth4 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-5,getdate())),01)

IF (DatePart(mm,getdate()) > 6)
        SET @PreviousMonth5 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-6,getdate())),01)
    Else
        SET @PreviousMonth5 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-6,getdate())),01)

IF (DatePart(mm,getdate()) > 7)
        SET @PreviousMonth6 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-7,getdate())),01)
    Else
        SET @PreviousMonth6 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-7,getdate())),01)

IF (DatePart(mm,getdate()) > 8)
        SET @PreviousMonth7 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-8,getdate())),01)
    Else
        SET @PreviousMonth7 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-8,getdate())),01)

IF (DatePart(mm,getdate()) > 9)
        SET @PreviousMonth8 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-9,getdate())),01)
    Else
        SET @PreviousMonth8 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-9,getdate())),01)

IF (DatePart(mm,getdate()) > 10)
        SET @PreviousMonth9 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-10,getdate())),01)
    Else
        SET @PreviousMonth9 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-10,getdate())),01)

IF (DatePart(mm,getdate()) > 11)
        SET @PreviousMonth10 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-11,getdate())),01)
    Else
        SET @PreviousMonth10 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-11,getdate())),01)

IF (DatePart(mm,getdate()) > 12)
        SET @PreviousMonth11 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-12,getdate())),01)
    Else
        SET @PreviousMonth11 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-12,getdate())),01)

IF (DatePart(mm,getdate()) > 13)
        SET @PreviousMonth12 = DateFromParts(datepart(yyyy,getdate()),month(dateadd(Month,-13,getdate())),01)
    Else
        SET @PreviousMonth12 = DateFromParts(datepart(yyyy,getdate())-1,month(dateadd(Month,-13,getdate())),01)


SET @CurrentMonthName = DateName(Month,getdate())
SET @PreviousMonthName = DateName(Month,@PreviousMonth)
SET @PreviousMonthName1 = DateName(Month,@PreviousMonth1)
SET @PreviousMonthName2 = DateName(Month,@PreviousMonth2)
SET @PreviousMonthName3 = DateName(Month,@PreviousMonth3)
SET @PreviousMonthName4 = DateName(Month,@PreviousMonth4)
SET @PreviousMonthName5 = DateName(Month,@PreviousMonth5)
SET @PreviousMonthName6 = DateName(Month,@PreviousMonth6)
SET @PreviousMonthName7 = DateName(Month,@PreviousMonth7)
SET @PreviousMonthName8 = DateName(Month,@PreviousMonth8)
SET @PreviousMonthName9 = DateName(Month,@PreviousMonth9)
SET @PreviousMonthName10 = DateName(Month,@PreviousMonth10)
SET @PreviousMonthName11 = DateName(Month,@PreviousMonth11)
SET @PreviousMonthName12 = DateName(Month,@PreviousMonth12)

SET @CurrentMonthString = Cast(@CurrentMonth as VarChar(20))
SET @PreviousMonthString = Cast(@PreviousMonth as VarChar(20))
SET @PreviousMonthString1 = Cast(@PreviousMonth1 as VarChar(20))
SET @PreviousMonthString2 = Cast(@PreviousMonth2 as VarChar(20))
SET @PreviousMonthString3 = Cast(@PreviousMonth3 as VarChar(20))
SET @PreviousMonthString4 = Cast(@PreviousMonth4 as VarChar(20))
SET @PreviousMonthString5 = Cast(@PreviousMonth5 as VarChar(20))
SET @PreviousMonthString6 = Cast(@PreviousMonth6 as VarChar(20))
SET @PreviousMonthString7 = Cast(@PreviousMonth7 as VarChar(20))
SET @PreviousMonthString8 = Cast(@PreviousMonth8 as VarChar(20))
SET @PreviousMonthString9 = Cast(@PreviousMonth9 as VarChar(20))
SET @PreviousMonthString10 = Cast(@PreviousMonth10 as VarChar(20))
SET @PreviousMonthString11 = Cast(@PreviousMonth11 as VarChar(20))
SET @PreviousMonthString12 =  Cast(@PreviousMonth12 as VarChar(20))

Set @CurrentMonthStringEnd = Cast(Dateadd(Day,-365,getDate()) as VarChar(20))
Set @PreviousMonthStringEnd = Cast(Dateadd(Day,-365,@previousMonth) as VarChar(20))
Set @PreviousMonthStringEnd1 = Cast(Dateadd(Day,-365,@previousMonth1) as VarChar(20))
Set @PreviousMonthStringEnd2 = Cast(Dateadd(Day,-365,@previousMonth2) as VarChar(20))
Set @PreviousMonthStringEnd3 = Cast(Dateadd(Day,-365,@previousMonth3) as VarChar(20))
Set @PreviousMonthStringEnd4 = Cast(Dateadd(Day,-365,@previousMonth4) as VarChar(20))
Set @PreviousMonthStringEnd5 = Cast(Dateadd(Day,-365,@previousMonth5) as VarChar(20))
Set @PreviousMonthStringEnd6 = Cast(Dateadd(Day,-365,@previousMonth6) as VarChar(20))
Set @PreviousMonthStringEnd7 = Cast(Dateadd(Day,-365,@previousMonth7) as VarChar(20))
Set @PreviousMonthStringEnd8 = Cast(Dateadd(Day,-365,@previousMonth8) as VarChar(20))
Set @PreviousMonthStringEnd9 = Cast(Dateadd(Day,-365,@previousMonth9) as VarChar(20))
Set @PreviousMonthStringEnd10 = Cast(Dateadd(Day,-365,@previousMonth10) as VarChar(20))
Set @PreviousMonthStringEnd11 = Cast(Dateadd(Day,-365,@previousMonth11) as VarChar(20))
Set @PreviousMonthStringEnd12 = Cast(Dateadd(Day,-365,@previousMonth12) as VarChar(20))

Declare @Formula as VarChar(100)
Declare @From as Varchar(200)
Declare @Where as Varchar(100)

Set @Formula = 'Cast(Round((COUNT (Distinct wojob.AssetID) *365.0)/(COUNT(*)),2) as Numeric(36,2))'
Set @From = ' FROM [Maint5k].[dbo].[woJob] INNER JOIN Maint5k.dbo.syJobType ON woJob.JobTypeID = syJobType.JobTypeID INNER JOIN Maint5k.dbo.pmAsset ON woJob.AssetID = pmAsset.AssetID'
Set @Where = ' and (pmAsset.Usertext2 = ''Critical'' or pmAsset.Usertext2 = ''Constraint'')'

Declare @Select12 as VarChar(MAX)
Declare @Select11 as VarChar(MAX)
Declare @Select10 as VarChar(MAX)
Declare @Select9 as VarChar(MAX)
Declare @Select8 as VarChar(MAX)
Declare @Select7 as VarChar(MAX)
Declare @Select6 as VarChar(MAX)
Declare @Select5 as VarChar(MAX)
Declare @Select4 as VarChar(MAX)
Declare @Select3 as VarChar(MAX)
Declare @Select2 as VarChar(MAX)
Declare @Select1 as VarChar(MAX)
Declare @Select as VarChar(MAX)
Declare @SelectCurrent as VarChar(MAX)


SET @Select12 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString12 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd12 + '''' + @Where + ')' + ' as ' + @PreviousMonthName12
SET @Select11 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString11 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd11 + '''' + @Where + ')' + ' as ' + @PreviousMonthName11
SET @Select10 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString10 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd10 + '''' + @Where + ')' + ' as ' + @PreviousMonthName10
SET @Select9 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString9 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd9 + '''' + @Where + ')' + ' as ' + @PreviousMonthName9
SET @Select8 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString8 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd8 + '''' + @Where + ')' + ' as ' + @PreviousMonthName8
SET @Select7 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString7 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd7 + '''' + @Where + ')' + ' as ' + @PreviousMonthName7
SET @Select6 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString6 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd6 + '''' + @Where + ')' + ' as ' + @PreviousMonthName6
SET @Select5 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString5 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd5 + '''' + @Where + ')' + ' as ' + @PreviousMonthName5
SET @Select4 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString4 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd4 + '''' + @Where + ')' + ' as ' + @PreviousMonthName4
SET @Select3 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString3 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd3 + '''' + @Where + ')' + ' as ' + @PreviousMonthName3
SET @Select2 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString2 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd2 + '''' + @Where + ')' + ' as ' + @PreviousMonthName2
SET @Select1 = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString1 + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd1 + '''' + @Where + ')' + ' as ' + @PreviousMonthName1
SET @Select = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @PreviousMonthString + ''' and woJob.StartDate > ''' + @PreviousMonthStringEnd + '''' + @Where + ')' + ' as ' + @PreviousMonthName
SET @SelectCurrent = ' (Select ' + @Formula + @From + ' Where syJobType.IsBreakDown = ''1'' and woJob.StartDate < ''' + @CurrentMonthString + ''' and woJob.StartDate > ''' + @CurrentMonthStringEnd + '''' + @Where + ')' + ' as ' + @CurrentMonthName

Print @SelectCurrent

Declare @Set1 as VarChar(Max)
Declare @Set2 as VarChar(Max)
Declare @Set3 as VarChar(Max)
Declare @Set4 as VarChar(Max)
Declare @Set5 as VarChar(Max)

Set @Set1 = @Select12 + ',' + @Select11 + ',' + @Select10 + ','
Set @Set2 = @Select9 + ',' + @Select8 + ',' + @Select7 + ','
Set @Set3 = @Select6 + ',' + @Select5 + ',' + @Select4 + ','
Set @Set4 = @Select3 + ',' + @Select2 + ',' + @Select1 + ',' + @Select --+ ','
Set @Set5 = @SelectCurrent



Print @SelectCurrent


Set @SQLString = 'Select ' + @Set1 + @Set2 + @Set3 + @Set4 --+ @Set5

Execute(@SQLString)