LoslegenKostenlos loslegen

Erstellen mehrerer Gruppen mit CASE

In dieser Übung schreibst du eine CASE Anweisung, um die Werte in der DurationSeconds in 5 Gruppen zu gruppieren, die auf den folgenden Bereichen basieren:

DurationSeconds SecondGroup
<= 120 1
(120) und (600) 2
(600) und (1200) 3
(1201) und (5000) 4
Für alle anderen Werte 5

Diese Übung ist Teil des Kurses

Intermediate SQL Server

Kurs anzeigen

Anleitung zur Übung

Erstelle eine neue Spalte, SecondGroup, die die Werte in der Spalte DurationSeconds basierend auf den oben genannten Bereichen verwendet.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

-- 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
Code bearbeiten und ausführen