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

  • Home
  • SEARCH
  • 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 8154483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:22:07+00:00 2026-06-06T16:22:07+00:00

Please help me generate the following query i’ve been struggling with for some time

  • 0

Please help me generate the following query i’ve been struggling with for some time now. Lets’ say I have a simple table with month number and information whether there were any failed events in this particular month

Below a script to generate sample data:

WITH DATA(Month, Success) AS
(
    SELECT  1, 0 UNION ALL
    SELECT  2, 0 UNION ALL
    SELECT  3, 0 UNION ALL
    SELECT  4, 1 UNION ALL
    SELECT  5, 1 UNION ALL
    SELECT  6, 0 UNION ALL
    SELECT  7, 0 UNION ALL
    SELECT  8, 1 UNION ALL
    SELECT  9, 0 UNION ALL
    SELECT 10, 1 UNION ALL
    SELECT 11, 0 UNION ALL
    SELECT 12, 1 UNION ALL
    SELECT 13, 0 UNION ALL
    SELECT 14, 1 UNION ALL
    SELECT 15, 0 UNION ALL
    SELECT 16, 1 UNION ALL
    SELECT 17, 0 UNION ALL
    SELECT 18, 0
)

Given the definition of a “repeated failure “:

When event failure occurs during at least 4 months in any 6 months period then the last month with such failure is a “repeated failure” my query should return the following output

Month   Success RepeatedFailure
1       0   
2       0   
3       0   
4       1   
5       1   
6       0       R1
7       0       R2
8       1   
9       0   
10      1   
11      0       R3
12      1   
13      0   
14      1   
15      0   
16      1   
17      0
18      0       R1

where:

  • R1 -1st repeated failure in month no 6 (4 failures in last 6 months).
  • R2 -2nd repeated failure in month no 7 (4 failures in last 6 months).
  • R3 -3rd repeated failure in month no 11 (4 failures in last 6 months).

R1 -again 1st repeated failure in month no 18 because Repeated Failures should be again numbered from the beginning when new Repeated Failure occurs for the first time in last 6 reporting periods

Repeated Failures are numerated consecutively because based on its number i must apply appropriate multiplier:

  • 1st repated failure – X2
  • 2nd repeated failure – X4
  • 3rd and more repeated failure -X5.
  • 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-06T16:22:08+00:00Added an answer on June 6, 2026 at 4:22 pm

    I’m sure this can be improved, but it works. We essentially do two passes – the first to establish repeated failures, the second to establish what kind of repeated failure each is. Note that Intermediate2 can definitely be done away with, I’ve only separated it out for clarity. All the code is one statement, my explanation is interleaved:

    ;WITH DATA(Month, Success) AS
    -- assuming your data  as defined (with my edit)
    ,Intermediate AS 
    (
    SELECT
        Month,
        Success,
        -- next column for illustration only
        (SELECT SUM(Success) 
         FROM DATA hist 
         WHERE curr.Month - hist.Month BETWEEN 0 AND 5) 
            AS SuccessesInLastSixMonths,
        -- next column for illustration only
        6 - (SELECT SUM(Success) 
         FROM DATA hist 
         WHERE curr.Month - hist.Month BETWEEN 0 AND 5) 
            AS FailuresInLastSixMonths,
        CASE WHEN 
                (6 - (SELECT SUM(Success) 
                        FROM DATA hist 
                        WHERE curr.Month - hist.Month BETWEEN 0 AND 5)) 
                >= 4 
                THEN 1
                ELSE 0 
        END AS IsRepeatedFailure
    FROM DATA curr 
    -- No real data until month 6
    WHERE curr.Month > 5
    )
    

    At this point we have established, for each month, whether it’s a repeated failure, by counting the failures in the six months up to and including it.

    ,Intermediate2 AS
    (
    SELECT 
        Month,
        Success,
        IsRepeatedFailure,
        (SELECT SUM(IsRepeatedFailure) 
            FROM Intermediate hist 
            WHERE curr.Month - hist.Month BETWEEN 0 AND 5) 
            AS RepeatedFailuresInLastSixMonths
    FROM Intermediate curr
    )
    

    Now we have counted the number of repeated failures in the six months leading up to now

    SELECT
        Month,
        Success,
        CASE IsRepeatedFailure 
            WHEN 1 THEN 'R' + CONVERT(varchar, RepeatedFailuresInLastSixMonths) 
            ELSE '' END
        AS RepeatedFailureText
    FROM Intermediate2
    

    so we can say, if this month is a repeated failure, what cardinality of repeated failure it is.

    Result:

    Month       Success     RepeatedFailureText
    ----------- ----------- -------------------------------
    6           0           R1
    7           0           R2
    8           1           
    9           0           
    10          1           
    11          0           R3
    12          1           
    13          0           
    14          1           
    15          0           
    16          1           
    17          0           
    18          0           R1
    
    (13 row(s) affected)
    

    Performance considerations will depend on on how much data you actually have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me to generate the following query. Say I have customer table and
I have used the following code to generate some dynamic checkboxes. This works for
How to auto generate id for child div in JQuery. Please help me so
Please help me with this update query. I COMPLETELY understand how redundant this is
Please help me to improve the following script: http://jsfiddle.net/n9BkM/8/ I need the following fucntionality:
Please help me, I'm trying to find some documentation about pre-installed applications at Android.
Please help me look into my simple test case: import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import
How can i generate multiple texts using the imagecreatetruecolor() method? I have the following
Some one please help me out of this ::( I am using ubuntu 11.04
Can you please help me with deciding which one of the following scenarios will

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.