2017-04-26 16 views
0

こんにちは私は以下のケースで問題があります。 は私が見つける「n」を1つのAPPLICATION_NAMEのステータスを失敗した必然的な結果として、それを返す必要が1列に同じ値を持つ複数の行

App_name  date   status 
Application 1 10-MAR-17 SUCCEEDED 
Application 1 11-MAR-17 SUCCEEDED 
Application 1 12-MAR-17 FAILED 
Application 1 13-MAR-17 SUCCEEDED 
Application 1 14-MAR-17 SUCCEEDED 
Application 1 15-MAR-17 FAILED 
Application 1 16-MAR-17 SUCCEEDED 
Application 1 17-MAR-17 SUCCEEDED 
Application 1 18-MAR-17 FAILED 
Application 1 19-MAR-17 SUCCEEDED 
Application 1 20-MAR-17 SUCCEEDED 
Application 1 21-MAR-17 FAILED 
Application 1 22-MAR-17 SUCCEEDED 
Application 1 23-MAR-17 SUCCEEDED 
Application 1 25-MAR-17 SUCCEEDED 
Application 3 20-MAR-17 FAILED 
Application 3 21-MAR-17 FAILED 
Application 3 22-MAR-17 FAILED 
Application 3 23-MAR-17 FAILED 
Application 3 24-MAR-17 FAILED 

ように、テーブル「アプリケーションログ」を持っています。たとえば、n = 5の場合、クエリはapp_name = Application 3を返す必要があります。

+0

あなたの質問私はそれは同様オラクルでは動作しない理由何らかの理由が表示されません十分に明確ではない – Mehr

+0

アプリケーション3も{0,1,2,3,4}の 'n 'に失敗しますか? –

+0

また、{0,1,2,3,4}のnに対しても失敗しますか? – MileP

答えて

2

次のクエリはSQL Serverで機能します。これは、単一のOLAP機能を利用し、別のアプローチである

SELECT [App_name] 
FROM (
    SELECT [App_name], [date], [status], 
      ROW_NUMBER() OVER (PARTITION BY App_name ORDER BY [date]) 
      - 
      ROW_NUMBER() OVER (PARTITION BY App_name, 
              CASE 
              WHEN status = 'FAILED' THEN 1 
              ELSE 2 
              END 
          ORDER BY [date]) AS grp 
    FROM ApplicationLog) AS t 
WHERE status = 'FAILED' 
GROUP BY [App_name], grp 
HAVING COUNT(*) >= 5 

Demo here

1

SELECT App_name, Max(date) AS LastFail 
FROM 
(
    SELECT App_name, date, status, 
     -- check if the last 5 rows only contain FAILED 
      Min(CASE WHEN status = 'FAILED' THEN 1 ELSE 0 end) 
      Over (PARTITION BY App_name 
       ORDER BY date 
     -- caution, as this includes the current row it must be n-1 
       ROWS 4 Preceding) AS x 
    FROM ApplicationLog 
) AS t 
WHERE x = 1 
GROUP BY App_name 
関連する問題