Crearea mai multor grupuri cu CASE
În acest exercițiu, vei scrie o instrucțiune CASE pentru a împărți valorile din coloana DurationSeconds în 5 grupuri, pe baza următoarelor intervale:
| DurationSeconds | SecondGroup |
|---|---|
| <= 120 | 1 |
| > 120 și <= 600 | 2 |
| > 600 și <= 1200 | 3 |
| > 1201 și <= 5000 | 4 |
| Pentru toate celelalte valori | 5 |
Acest exercițiu face parte din cursul
SQL Server intermediar
Instrucțiuni pentru exercițiu
Creează o coloană nouă, SecondGroup, care să atribuie valori din coloana DurationSeconds pe baza intervalelor menționate mai sus.
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
-- 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