始める無料で始める

CASE で複数のグループを作成する

この演習では、DurationSeconds の値を次の範囲に基づいて5つのグループに分ける CASE ステートメントを書きます。

DurationSeconds SecondGroup
<= 120 1
> 120 and <= 600 2
> 600 and <= 1200 3
> 1201 and <= 5000 4
For all other values 5

この演習はコースの一部です

中級 SQL Server

コースを見る

演習の手順

上記の範囲に基づき、DurationSeconds 列の値を使用して新しい列 SecondGroup を作成してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

-- Complete the syntax for cutting the duration into different cases
SELECT DurationSeconds, 
-- Start with the 2 TSQL keywords, and after the condition a TSQL word and a value
      ___ ___ (DurationSeconds <= 120) ___ ___
-- The pattern repeats with the same keyword and after the condition the same word and next value          
       ___ (DurationSeconds > 120 AND DurationSeconds <= 600) ___ ___
-- Use the same syntax here             
       ___ (DurationSeconds > 601 AND DurationSeconds <= 1200) ___ ___
-- Use the same syntax here               
       ___ (DurationSeconds > 1201 AND DurationSeconds <= 5000) ___ ___
-- Specify a value      
       ELSE ___ 
       END AS SecondGroup   
FROM Incidents
コードを編集して実行