0

I need help creating a table using the pivot clause to include the average salary of each Division_ID and use Division_ID as a Row and Job_ID as Column using data in employees2 table.

Here is my query

SELECT * 
FROM ( 
  SELECT JOB_ID, DIVISION_ID, SALARY
  FROM employees2
  WHERE DIVISION_ID IN (1, 2, 3, 4, 5)
  )
PIVOT ( 
AVG(SALARY) FOR JOB_ID IN (1 AS ENG, 2 AS MGR, 3 AS PRE, 4 AS WOR, 5 AS TEC)
)
ORDER BY DIVISION _ID;

I get the following error when I try to execute the statement

ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
Error at Line: 6 Column: 3

Here is the data that is in employees2 table (note their are only 5 JOB_IDs)

employees2 table

Finally, here is an example of how my result should look like

example result

4

1 回答 1

1

给定您的示例数据,您的查询正在检查intpartition_ids 和 job_ids,当它出现时,它们是varchar字段。

这是一个工作版本:

SELECT * 
FROM ( 
  SELECT JOB_ID, DIVISION_ID, SALARY
  FROM employees2
  WHERE DIVISION_ID IN ('div1','div2')
  ) 
PIVOT ( 
  AVG(SALARY) FOR JOB_ID IN ('job1', 'job2')
) 
ORDER BY 1
于 2013-10-28T00:24:57.767 回答