Oracle的table()函数是一种将集合类型转换为关系表的方法,它可以在复杂查询中发挥作用
示例:计算每个部门的员工平均工资,并按部门排序。
SELECT department_id, AVG(salary) OVER (PARTITION BY department_id) as avg_salary
FROM table(employees);
示例:将员工的工资信息按月份进行展示。
SELECT *
FROM table(employees)
PIVOT (
SUM(salary) FOR month IN ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
);
示例:查询组织结构中的所有经理及其直接下属。
SELECT employee_id, manager_id, level
FROM table(employees)
CONNECT BY PRIOR employee_id = manager_id
START WITH employee_id = 100;
示例:查询平均工资高于部门平均工资的员工。
SELECT employee_id, salary
FROM table(employees)
WHERE salary > (
SELECT AVG(salary)
FROM table(employees) e2
WHERE e2.department_id = employees.department_id
);
总之,在复杂查询中,table()函数可以与多种SQL操作结合使用,实现更高效、更灵活的数据处理和分析。