如何通过函数使用扩展组来获取此查询

时间:2014-05-08 03:06:46

标签: sql oracle sqlplus

我正在尝试编写一个返回此内容的查询:

DNAME                                    G TITLE            SALARY
---------------------------------------- - ------------ ----------
                                                           1059000
                                           Analyst          107000
                                           Manager          395000
                                           Developer        152000
                                           Programmer       152500
                                           Sr. Analyst      252500
                                         F                  366500
                                         M                  692500
Risk and Compliance                                         264500
Finance and Accounting Excellence                           395000
Internal Audit and Financial Controls                       399500

11 rows selected.

更新!!! 好的所以我正在玩不同的查询,这是我对上述输出最接近的......看起来它仍然缺少所有工资的总和第一次返回行...我相信为了使生成的输出以该格式显示是通过使用多维数据集组功能,但是我尝试了不同组合的逐组多维数据集无效。 ..任何想法???

  

从(选择dname,性别,标题,选择dname,性别,职称,薪水)   grouping_id(dname,gender,title)as" group_id",sum(salary)as azdepartment的薪水   通过分组集(dname,在azdepartment.did = azconsultant.did组中加入azconsultant   性别,标题)有grouping_id(dname,性别,标题)> 0);

DNAME                                    G TITLE            SALARY
---------------------------------------- - ------------ ----------
                                           Manager          395000
                                           Programmer       152500
                                           Sr. Analyst      252500
                                           Analyst          107000
                                           Developer        152000
Finance and Accounting Excellence                           395000
Risk and Compliance                                         264500
Internal Audit and Financial Controls                       399500
                                         M                  692500
                                         F                  366500

10 rows selected.

1 个答案:

答案 0 :(得分:1)

使用UNION s:

完成
  select '' as dname, '' as gender, '' as title, sum(salary) as salary 
  from azconsultant
union 
  select '' as dname, '' as gender,       title, sum(salary) as salary 
  from azconsultant
  group by title 
union 
  select '' as dname,       gender, '' as title, sum(salary) as salary 
  from azconsultant
  group by gender 
union 
  select dname,       '' as gender, '' as title, sum(salary) as salary 
  from azdepartment 
  join azconsultant on azdepartment.did=azconsultant.did 
  group by dname
order by 1 DESC, 2 DESC , 3 DESC;

更新已添加ORDER BY,已测试并添加了小提琴:http://sqlfiddle.com/#!4/3fa8b/4