Bắt đầu ngayBắt đầu miễn phí

Tạo nhiều nhóm với CASE

Trong bài tập này, bạn sẽ viết một câu lệnh CASE để phân nhóm các giá trị trong DurationSeconds thành 5 nhóm dựa trên các khoảng sau:

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

Bài tập này là một phần của khóa học

SQL Server trung cấp

Xem khóa học

Hướng dẫn bài tập

Tạo một cột mới, SecondGroup, sử dụng các giá trị trong cột DurationSeconds theo các khoảng đã nêu ở trên.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

-- 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
Chỉnh sửa và Chạy Mã