SQL CASE 语句有问题
SQL issue with CASE statement
我正在尝试调整以下语句:
SELECT CASE WHEN OrganizationLevel < 2 THEN UPPER(JobTitle)
ELSE JobTitle END as 'Job Title', COUNT(BusinessEntityID) as 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle, OrganizationLevel
ORDER BY JobTitle ASC
我需要更改它,以便在 OrganisationLevel 为 1 时使 JobTitle 显示为 'SOMETHING ELSE'。
我认为这很简单,只需对 CASE 语句进行一些小的更改,这样 JobTitle 就会是 = 'SOMETHING ELSE',但它不允许我这样做,
SELECT CASE WHEN OrganizationLevel < 2 THEN UPPER(JobTitle)
ELSE WHEN OrganizationLevel =1 THEN 'SOMETHING ELSE' ELSE JobTitle END as 'Job Title', COUNT(BusinessEntityID) as 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle, OrganizationLevel
ORDER BY JobTitle ASC
您需要在 Group by
中有相同的 case
语句
SELECT CASE
WHEN OrganizationLevel = 1 THEN 'SOMETHING_ELSE'
WHEN OrganizationLevel < 2 THEN Upper(JobTitle)
ELSE JobTitle
END AS 'Job Title',
Count(BusinessEntityID) AS 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle,
CASE
WHEN OrganizationLevel = 1 THEN 'SOMETHING_ELSE'
WHEN OrganizationLevel < 2 THEN Upper(JobTitle)
ELSE JobTitle
END
ORDER BY JobTitle ASC
我正在尝试调整以下语句:
SELECT CASE WHEN OrganizationLevel < 2 THEN UPPER(JobTitle)
ELSE JobTitle END as 'Job Title', COUNT(BusinessEntityID) as 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle, OrganizationLevel
ORDER BY JobTitle ASC
我需要更改它,以便在 OrganisationLevel 为 1 时使 JobTitle 显示为 'SOMETHING ELSE'。
我认为这很简单,只需对 CASE 语句进行一些小的更改,这样 JobTitle 就会是 = 'SOMETHING ELSE',但它不允许我这样做,
SELECT CASE WHEN OrganizationLevel < 2 THEN UPPER(JobTitle)
ELSE WHEN OrganizationLevel =1 THEN 'SOMETHING ELSE' ELSE JobTitle END as 'Job Title', COUNT(BusinessEntityID) as 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle, OrganizationLevel
ORDER BY JobTitle ASC
您需要在 Group by
case
语句
SELECT CASE
WHEN OrganizationLevel = 1 THEN 'SOMETHING_ELSE'
WHEN OrganizationLevel < 2 THEN Upper(JobTitle)
ELSE JobTitle
END AS 'Job Title',
Count(BusinessEntityID) AS 'number of employees'
FROM HumanResources.Employee
WHERE OrganizationLevel < 3
GROUP BY JobTitle,
CASE
WHEN OrganizationLevel = 1 THEN 'SOMETHING_ELSE'
WHEN OrganizationLevel < 2 THEN Upper(JobTitle)
ELSE JobTitle
END
ORDER BY JobTitle ASC