Skapa flera grupper med CASE
I den här övningen ska du skriva ett CASE-uttryck för att gruppera värdena i DurationSeconds i 5 grupper baserat på följande intervall:
| DurationSeconds | SecondGroup |
|---|---|
| <= 120 | 1 |
| > 120 och <= 600 | 2 |
| > 600 och <= 1200 | 3 |
| > 1201 och <= 5000 | 4 |
| För alla övriga värden | 5 |
Den här övningen är en del av kursen
SQL Server för medelnivå
Övningsinstruktioner
Skapa en ny kolumn, SecondGroup, som använder värdena i kolumnen DurationSeconds baserat på de intervall som anges ovan.
Interaktiv övning med praktiskt arbete
Testa den här övningen genom att slutföra den här exempelkoden.
-- 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