Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot Table Error 56901 non-constant expression

I need to create a pivot table of average salary in Division_ID as Row and Job_ID as Column using data in employees2 table. So far I have:

SELECT * 
FROM (
SELECT Division_ID, Salary, Job_ID
FROM Employees2
)
PIVOT (
AVG(Salary) FOR Job_ID IN (ENG, TEC, MGR)
)
ORDER BY Job_ID

I get the error, "non-constant expression is not allowed for pivot|unpivot values"

What am I doing wrong? Most the examples I can find online deal with Numerical values after the IN statement, do I need to do something different since im working with text/string?

like image 837
user3729062 Avatar asked Dec 10 '25 05:12

user3729062


1 Answers

Use below Query.

SELECT *    FROM (
    SELECT Division_ID, Salary, Job_ID
    FROM Employees2
)  
PIVOT 
(
     AVG(Salary) FOR Job_ID IN 
    ('ENG', 'TEC', 'MGR')     ------------> Change done here.....
)
ORDER BY Job_ID

The values ENG, TEC and MGR need to be surrounded by single quotes to be considered as constant values.

like image 62
Nishanthi Grashia Avatar answered Dec 12 '25 19:12

Nishanthi Grashia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!