Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:21:00+00:00 2026-06-02T22:21:00+00:00

I have a table with four columns; Plant_Id, Year, Month and MR. I would

  • 0

I have a table with four columns; Plant_Id, Year, Month and MR. I would like to be able to calculate the Range (month 2 month difference) of MR.
A sample of the table (MaintenanceRatebepaen is the table name) looks like this:

Plant_ID  Year  Month  MR  
CCAR      2009    1    0,706452  
CCAR      2009    2    0,625899  
CCAR      2009    3    0,636678  
CCAR      2009    4    0,736544  
CCAR      2009    5    0,552023
CCAR      2009    6    0,418338  
CCAR      2009    7    0,502732  
CCAR      2009    8    0,64526  
CCAR      2009    9    0,743333  
CCAR      2009    10   0,555556  
CCAR      2009    11   0,297561  
CCAR      2009    12   0,338608  
CCAR      2010    1    0,380783  
Etc.

I’m not sure this is of interest, but Plant_id can have 25 different values, years from 2008 – 2012, months 1-12, MR is a calculated value.

The Query looks like this:

SELECT  Plant_Id, Jaar, Maand
    ,   (SUM(Compl) + 0.000) / SUM(Total) AS MR  
FROM (
        SELECT  Plant_Id, Jaar, Late, EarlyJobs, OnTimeJobs, Maand
            ,   SUM(EarlyJobs + OnTimeJobs) AS Compl
            ,   SUM(EarlyJobs) + SUM(Late) + SUM(OnTimeJobs) AS Total 
        FROM    MaintenanceRatebepaen AS MaintenanceRatebepaen_1  
        GROUP BY Plant_Id, Jaar, Maand, Late, OnTimeJobs, EarlyJobs
    ) AS MaintenanceRatebepaen  
WHERE   (Jaar >= 2009) AND (Jaar <= 2011) AND (Plant_Id = 'CCAR')   
GROUP BY Jaar, Plant_Id, Maand  
ORDER BY Plant_Id, Jaar, Maand

I’m new to SQL, I managed to get the above from books and google searches. But I can’t get the Range calculated, any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:21:03+00:00Added an answer on June 2, 2026 at 10:21 pm

    Did you just want month by month differences?

    Data

    declare @data table (PlantId nvarchar(5), [Year] nvarchar(4), [Month] int, MR decimal(10,10))
    
    INSERT @data VALUES 
    ('CCAR','2009','1','0.706452'),('CCAR','2009','2','0.625899'),('CCAR','2009','3','0.636678'),('CCAR','2009','4','0.736544'),('CCAR','2009','5','0.552023'),('CCAR','2009','6','0.418338'),('CCAR','2009','7','0.502732'),('CCAR','2009','8','0.64526'),('CCAR','2009','9','0.743333'),('CCAR','2009','10','0.555556'),('CCAR','2009','11','0.297561'),('CCAR','2009','12','0.338608'),('CCAR','2010','1','0.380783')
    

    Query

    ;with cte as (
        SELECT *, ROW_NUMBER() OVER (ORDER BY Year DESC, Month DESC) AS RowNumber  FROM @data
    )
    select  d1.PlantId
        ,   d1.Year
        ,   d1.Month
        ,   d1.MR
        ,   d1.MR-d2.MR AS [ChangeMRFromPreviousMonth]
    from    cte d1
    LEFT OUTER JOIN cte d2
        on d2.RowNumber = (d1.RowNumber+1)
    order by d1.RowNumber DESC
    

    Results:

    PlantId Year Month       MR                                      ChangeMRFromPreviousMonth
    ------- ---- ----------- --------------------------------------- ---------------------------------------
    CCAR    2009 1           0.7064520000                            NULL
    CCAR    2009 2           0.6258990000                            -0.0805530000
    CCAR    2009 3           0.6366780000                            0.0107790000
    CCAR    2009 4           0.7365440000                            0.0998660000
    CCAR    2009 5           0.5520230000                            -0.1845210000
    CCAR    2009 6           0.4183380000                            -0.1336850000
    CCAR    2009 7           0.5027320000                            0.0843940000
    CCAR    2009 8           0.6452600000                            0.1425280000
    CCAR    2009 9           0.7433330000                            0.0980730000
    CCAR    2009 10          0.5555560000                            -0.1877770000
    CCAR    2009 11          0.2975610000                            -0.2579950000
    CCAR    2009 12          0.3386080000                            0.0410470000
    CCAR    2010 1           0.3807830000                            0.0421750000
    

    Is that what you needed? Your query contains alot of extra columns, so I don’t know if you want that including?

    * Edit *
    In response to your comments, you need to put your query inside the CTE, with the additional RowNumber column. d1 and d2 are just aliases of that CTE. I think this should do it:

    ;with cte as (
        SELECT  Plant_Id, Jaar, Maand
            ,   (SUM(Compl) + 0.000) / SUM(Total) AS MR  
            ,   ROW_NUMBER() OVER (ORDER BY Jaar DESC, Maand DESC) AS RowNumber
        FROM (
            SELECT  Plant_Id, Jaar, Late, EarlyJobs, OnTimeJobs, Maand
                ,   SUM(EarlyJobs + OnTimeJobs) AS Compl
                ,   SUM(EarlyJobs) + SUM(Late) + SUM(OnTimeJobs) AS Total 
            FROM    MaintenanceRatebepaen AS MaintenanceRatebepaen_1  
            GROUP BY Plant_Id, Jaar, Maand, Late, OnTimeJobs, EarlyJobs
        ) AS MaintenanceRatebepaen  
        WHERE   (Jaar >= 2009) AND (Jaar <= 2011) AND (Plant_Id = 'CCAR')   
        GROUP BY Jaar, Plant_Id, Maand  
        ORDER BY Plant_Id, Jaar, Maand
    )
    select  d1.PlantId
        ,   d1.Jaar
        ,   d1.Maand
        ,   d1.MR
        ,   d1.MR-d2.MR AS [ChangeMRFromPreviousMonth]
    from    cte d1
    LEFT OUTER JOIN cte d2
        on d2.RowNumber = (d1.RowNumber+1)
    order by d1.RowNumber DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with four columns, and I would like each column head
In my table accounts I have four columns like user, pass, column1, column2 I
I have a table for image gallery with four columns like: foid | uid
I have a table with four Columns: Col1, Col2, Col3, and Col4. Col1, Col2,
I have a fairly static (InnoDB) table T with four columns: A , B
i have four tables like below in sql server 2008 : TABLE 1 ->
I have a table with four columns, where col1 & col2 contain similar values
I am a student this is homework... I have one table with four columns:
i am using sql server 2000 .I have a table with four columns ie.
I have a database table has four columns called Courses(CourseID,CourseName,CreditHours,LabSession) , and I wanna

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.