2012-06-25 8 views
6

Per favore aiutami a generare la seguente query con cui sto lottando da un po 'di tempo. Lets' dire che ho una semplice tabella con il numero e le informazioni mese se ci fossero eventi falliti in questo mese particolareRicerca di occorrenze ripetute con funzioni di classifica

Qui di seguito uno script per generare dati di esempio:

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 
) 

Data la definizione di un "fallimento ripetuto ":

Quando si verifica un errore durante l'evento di almeno 4 mesi in un periodo di 6 mesi poi il mese scorso con tale fallimento è un 'fallimento ripetuto' la mia domanda dovrebbe restituire il seguente 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 
.515.053.691,36321 milioni

dove:

  • R1 -1 ° ripetuti fallimenti nel mese 6 (4 fallimenti negli ultimi 6 mesi).
  • R2 -2nd ripetuto guasto nel mese n. 7 (4 guasti negli ultimi 6 mesi).
  • R3 -3 ripetuto errore nel mese n. 11 (4 guasti negli ultimi 6 mesi).

R1 -nuovamente primo ripetuti fallimenti nel mese no 18 a causa ripetuti fallimenti dovrebbero essere di nuovo numerate dall'inizio quando si verifica nuova ripetuti fallimenti per la prima volta negli ultimi 6 periodi di riferimento

ripetuti fallimenti sono numerati consecutivamente a causa in base al suo numero devo applicare appropriata moltiplicatore:

  • primo fallimento ripetuti, - X2
  • secondo ripetuti fallimenti - X4
  • 3o errore più ripetuto -X5.
+0

Quale versione di SQL Server? Il 2012 ha funzioni di ranking aggiuntive rispetto al 2005-2008. –

+0

Ciao, stiamo usando SQL Server 2008 –

+0

Ho modificato i tuoi dati per (penso) risolverlo - ti preghiamo di modificare ulteriormente se ho fatto un errore – AakashM

risposta

2

Sono sicuro che questo può essere migliorato, ma funziona. Effettuiamo essenzialmente due passaggi: il primo per stabilire i guasti ripetuti, il secondo per stabilire quale tipo di errore ripetuto sia. Si noti che lo Intermediate2 può essere definitivamente eliminato, l'ho separato solo per chiarezza. Tutto il codice è una dichiarazione, la mia spiegazione è intercalati:

;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 
) 

A questo punto abbiamo stabilito, per ogni mese, che si tratti di un fallimento ripetuto, contando i fallimenti nei sei mesi fino al esso.

,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 
) 

ora abbiamo contato il numero di ripetuto fallimenti nei sei mesi che conduce fino ad ora

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

così possiamo dire, se questo mese è un fallimento ripetuto, cosa cardinalità di ripetuti fallimento lo è.

Risultato:

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) 

Considerazioni sulle prestazioni dipenderanno dalla quantità di dati che effettivamente hanno.

+0

Grazie AakashM. Il lavoro è come un fascino –

2
;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 
) 

SELECT DATA.Month,DATA.Success,Isnull(convert(Varchar(10),b.result),'') +   
Isnull(CONVERT(varchar(10),b.num),'') RepeatedFailure 
FROM (
SELECT *, ROW_NUMBER() over (order by Month) num FROM 
(Select * ,(case when (select sum(Success) 
from DATA where MONTH>(o.MONTH-6) and MONTH<=(o.MONTH) ) <= 2 
and o.MONTH>=6 then 'R' else '' end) result 
from DATA o 
) a where result='R' 
) b 
right join DATA on DATA.Month = b.Month 
order by DATA.Month 
+0

Asif è una soluzione semplice ma non restituisce l'output desiderato. Nel 18 ° mese c'è un errore ripetuto ma non R4. Dovrebbe essere R1. È la prima occorrenza di un errore ripetuto negli ultimi 6 mesi (13-18) –

Problemi correlati