Home Forums SQL Server 2008 T-SQL (SS2K8) Convert the stored procedure to "Standard SQL Select" RE: Convert the stored procedure to "Standard SQL Select"

  • Jeff Moden (4/12/2014)


    Pesonally, I think that option 3 is the worst (it sounds like you're setting up to write embedded code, which is a huge mistake for maintenance, IMHO).

    I totally agree with you on this, however Option 3 sometimes is the best approach indeed: please just think about if it takes you two weeks or longer to implement any change in production. Option 3 doesn't ask you to go through the painful process, this is the beautiful advantage! Option 2 is best in terms of development while Option 1 is the simplest one.

    Option 2 would be the best but it's your problem. Here's how you can do option 3 and still achieve what I was talking about.

    WITH

    cteFYStart AS (SELECT FYStart = DateAdd(mm,-2,DateAdd(yy,DateDiff(yy,0,DateAdd(mm,2,Getdate())),0)))

    ,cteFYAll AS (SELECT FYStart

    ,FYNextStart = DateAdd(yy,1,FYStart)

    ,FYPrevStart = DateAdd(yy,-1,FYStart)

    FROM cteFYStart)

    SELECTITSDivisionID

    ,A = Sum(Case When d.DocStatus = 'Retired' and d.RetiredDate >= fy.FYPrevStart and d.RetiredDate < fy.FYStart Then 1 Else 0 End)

    ,B = Sum(Case When d.DocStatus = 'Active' Then 1 Else 0 End)

    ,C = Sum(Case When d.DocStatus = 'Active' and d.TargetClosureDate >= fy.FYStart and d.TargetClosureDate < fy.FYNextStart Then 1 Else 0 End)

    ,D = Sum(Case When d.DocStatus = 'Active' and d.TargetClosureDate >= fy.FYNextStart Then 1 Else 0 End)

    ,Critical = Sum(Case When d.DocStatus = 'Active' and d.RiskRating = 4 Then 1 Else 0 End)

    ,High = Sum(Case When d.DocStatus = 'Active' and d.RiskRating = 3 Then 1 Else 0 End)

    ,Medium = Sum(Case When d.DocStatus = 'Active' and d.RiskRating = 2 Then 1 Else 0 End)

    ,Low = Sum(Case When d.DocStatus = 'Active' and d.RiskRating = 1 Then 1 Else 0 End)

    ,TBD = Sum(Case When d.DocStatus = 'Active' and d.RiskRating = 0 Then 1 Else 0 End)

    FROM dbo.Document d

    CROSS JOIN cteFYAll fy

    WHERE DocType = 1

    AND DocStatus IN ('Active','Retired')

    GROUP BY ITSDivisionID

    ;

    Again, the code isn't yet complete. I'm waiting for you to think outside the box a bit and make it so BCD and DRR are calculated without repeating code and to add the "Division" name column using just one more CTE.

    Your code works perfectly in SQL, I have to think about how to make DRR, weekend is full to me. Your code is still not accepted by Dundas (because of the CTE), I will check with them to see if there could be any work around. Thank you once again for your kind help and patient. Have a break and enjoy your weekend with your family.