ALTER TABLE person
ADD COLUMN Stay_Length VARCHAR(50);
ALTER TABLE person
ADD COLUMN Time_the_person_stayed_in_the_city VARCHAR(50);
INSERT INTO Person (Stay_Length)
VALUES ('Short'), ('Long'),('Very Long');
INSERT INTO Person (Time_the_person_stayed_in_the_city)
VALUES ('Less than 20 days'), ('Between 20 and 99 days'),('Over 99 days ');
` SELECT Stay_Length, Time_the_person_stayed_in_the_city,
CASE Stay_Length
WHEN <20 THEN 'Short'
WHEN 20>=99 THEN 'Long'
WHEN >99 THEN 'Very Long'
ELSE 'Unknown Length of Stay'
END AS Stay_Length
FROM Person;`
You case statement is wrong
SELECT Stay_Length, Time_the_person_stayed_in_the_city,
CASE
WHEN Stay_Length <20 THEN 'Short'
WHEN Stay_Length BETWEEN 20 AND 99 THEN 'Long'
WHEN Stay_Length >99 THEN 'Very Long'
ELSE 'Unknown Length of Stay'
END AS Stay_Length
FROM Person;
或
SELECT Stay_Length, Time_the_person_stayed_in_the_city,
CASE
WHEN Stay_Length <20 THEN 'Short'
WHEN Stay_Length >= 20 AND Stay_Length <= 99 THEN 'Long'
WHEN Stay_Length >99 THEN 'Very Long'
ELSE 'Unknown Length of Stay'
END AS Stay_Length
FROM Person;
请将您的CASE声明修改如下:
SELECT Stay_Length, Time_the_person_stayed_in_the_city,
CASE
WHEN Stay_Length <20 THEN 'Short'
WHEN Stay_Length >= 20 AND Stay_Length <=99 THEN 'Long'
WHEN Stay_Length >99 THEN 'Very Long'
ELSE 'Unknown Length of Stay'
END AS Stay_Length
FROM Person;`