1

I'm trying to adjust the following 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

I need to change it so as to make JobTitle appear as 'SOMETHING ELSE' when the OrganisationLevel is 1.

I thought this would be a simple matter of making a small change to the CASE statement so JobTitle would be = 'SOMETHING ELSE' but it won't allow me to do this,

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1
   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
SouravA
  • 5,147
  • 2
  • 24
  • 49
1

You need to have same case statement in Group by

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 
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • close but It brings up an error : Column 'HumanResources.Employee.OrganizationLevel' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. – Xid Stargazer Mar 10 '15 at 15:59