分组不按预期工作

时间:2012-08-22 11:58:59

标签: oracle oracle11g plsqldeveloper

我遇到了GROUP BY子句的问题,数据没有像我想要的那样出现:

我有这样的数据:

-------------------------------------------------------------------
| Company   | Division | Business Area | Unit   | Name Full  | ID |
-------------------------------------------------------------------
| Company 1 | Div 1    | Business 1    | Unit 1 | Customer 1 | 01 |
| Company 1 | Div 1    | Business 1    | Unit 1 | Customer 2 | 02 |
| Company 1 | Div 1    | Business 1    | Unit 2 | Customer 3 | 03 |
| Company 1 | Div 1    | Business 3    | Unit 3 | Customer 2 | 02 |
| Company 1 | Div 2    | Business 1    | Unit 1 | Customer 4 | 04 |
....
....

在用户界面中,我想让它显示如下:

Company 1, Div 1, Business 1, Unit 1
   -- Customer 1  01
   -- Customer 2  02
Company 1, Div 1, Business 1, Unit 2
   -- Customer 3 03
Company 1, Div 1, Business 3, Unit 3
   -- Customer 2 02
Company 1, Div 2, Business 1, Unit 1
   -- Customer 4 04

我尝试了查询,如果我尝试仅使用公司,div,业务和单位进行分组,那么我得到错误:00979:不是按表达式分组,因为我没有使用SELECT中使用的所有列,但后来我没有得到我要求的结果。

知道我该怎么办?

6 个答案:

答案 0 :(得分:4)

Jaanna,

您的示例数据集有5行,您的预期输出包含9行。这些额外的4行是每个(公司,部门,business_area,单位)的聚合,其中name_full和id正在汇总。

您的示例数据:

SQL> create table mytable (company,division,business_area,unit,name_full,id)
  2  as
  3  select 'Company 1', 'Div 1', 'Business 1', 'Unit 1', 'Customer 1', '01' from dual union all
  4  select 'Company 1', 'Div 1', 'Business 1', 'Unit 1', 'Customer 2', '02' from dual union all
  5  select 'Company 1', 'Div 1', 'Business 1', 'Unit 2', 'Customer 3', '03' from dual union all
  6  select 'Company 1', 'Div 1', 'Business 3', 'Unit 3', 'Customer 2', '02' from dual union all
  7  select 'Company 1', 'Div 2', 'Business 1', 'Unit 1', 'Customer 4', '04' from dual
  8  /

Table created.

查询:

SQL> select company
  2       , division
  3       , business_area
  4       , unit
  5       , name_full
  6       , id
  7    from mytable
  8   group by company
  9       , division
 10       , business_area
 11       , unit
 12       , rollup((name_full,id))
 13   order by company
 14       , division
 15       , business_area
 16       , unit
 17       , grouping(name_full) desc
 18       , name_full
 19  /

COMPANY   DIVISION BUSINESS_AREA UNIT   NAME_FULL  ID
--------- -------- ------------- ------ ---------- --
Company 1 Div 1    Business 1    Unit 1
Company 1 Div 1    Business 1    Unit 1 Customer 1 01
Company 1 Div 1    Business 1    Unit 1 Customer 2 02
Company 1 Div 1    Business 1    Unit 2
Company 1 Div 1    Business 1    Unit 2 Customer 3 03
Company 1 Div 1    Business 3    Unit 3
Company 1 Div 1    Business 3    Unit 3 Customer 2 02
Company 1 Div 2    Business 1    Unit 1
Company 1 Div 2    Business 1    Unit 1 Customer 4 04

9 rows selected.

您可能想要有条件地显示某些列值,但我将其作为练习留给您。

的问候,
罗布。

答案 1 :(得分:2)

使用“分组依据”查询无法实现您要执行的操作。相反,您可以使用“order by”,如:

select * from table order by Company, Division, BusinessArea, Unit asc

希望有所帮助

编辑: 对不起,我无法评论弗洛林的回答。 Florin的答案也很棒,但是例如在Web UI上进一步处理它会更难。如果它是plsql并且输出应该与您在问题中写的完全相同,那么您应该使用循环来获取每个公司,Division,BusinessArea,Unit组的名称和ID。

答案 2 :(得分:1)

我不熟悉环境,但通常GROUP BY查询要求每列都是GROUP BY字段或聚合字段。

你想要达到的目标看起来更像是对我的ORDER BY。

希望这有帮助。

答案 3 :(得分:1)

select 
   company, 
   division, 
   businessarea, 
   unit, 
   LISTAGG(fullname||' '||id , '|') WITHIN GROUP (ORDER BY fullname)
from your_table
group by company, division, businessarea, unit
order by company, division, businessarea, unit

答案 4 :(得分:1)

select * from 

(
select Company, Division, BusinessArea,  Unit ,NameFull,ID 
       from t
union all
select distinct  Company, Division, BusinessArea,  Unit ,'' as NameFull,0 as ID 
       from t
) d 

order by Company, Division, BusinessArea,  Unit ,NameFull,ID

答案 5 :(得分:1)

如果您为公司/部门/业务范围/单位集中的每个成员添加计数,例如:

SELECT company,
    division,
    business_area,
    unit,
    name_full,
    ID,
    ROW_NUMBER () OVER (PARTITION BY company, division, business_area, unit 
    ORDER BY  company, division, business_area, unit, name_full) AS rn
FROM t1

这应该返回:

------------------------------------------------------------------------ 
| Company   | Division | Business Area | Unit   | Name Full  | ID | rn |  
------------------------------------------------------------------------ 
| Company 1 | Div 1    | Business 1    | Unit 1 | Customer 1 |  01 |1  |
| Company 1 | Div 1    | Business 1    | Unit 1 | Customer 2 |  02 |2  |
| Company 1 | Div 1    | Business 1    | Unit 2 | Customer 3 |  03 |1  |
| Company 1 | Div 1    | Business 3    | Unit 3 | Customer 2 |  02 |1  |
| Company 1 | Div 2    | Business 1    | Unit 1 | Customer 4 |  04 |1  |

然后在你的ui中你可以遍历结果并显示每个记录,其中rn = 1作为标题,然后是全名& id,这是一些psudo代码

for each record loop
 if rn=1 
   display t1.division, t1.business_area
 end if
 display t1.name_full,id
end loop