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.
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
Intermediate2can definitely be done away with, I’ve only separated it out for clarity. All the code is one statement, my explanation is interleaved: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.
Now we have counted the number of repeated failures in the six months leading up to now
so we can say, if this month is a repeated failure, what cardinality of repeated failure it is.
Result:
Performance considerations will depend on on how much data you actually have.