2012-08-22 95 views
1

我現在面臨一個問題,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 | 
.... 
.... 

在UI,我想有它顯示的是這樣的:

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中使用的那樣使用所有列,但是我沒有得到我所需的結果。

任何想法我可以如何繼續?

+0

這是不能直接解決使用只是一羣,請做一些更多的研究,如何在SQL中'group by'子句工作。正確的做法還包括在讀取數據的服務器環境中進行編碼,或者在前端使用JavaScript。 – Reimius

回答

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. 

您可能希望有條件地顯示一些列的值或沒有,但我將它作爲一個練習給你。

Regards,
Rob。

1

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

你想達到什麼看起來更像是一個ORDER BY給我。

希望這會有所幫助。

2

使用「group by」查詢無法實現您嘗試執行的操作。相反,你可以「按訂單」像使用:

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

希望它有助於

編輯: 抱歉,我不能讓上弗洛林的回答評論。例如,弗洛林的答案也很棒,但在Web UI上進一步處理會更困難。如果它是plsql,並且輸出應該與您在問題中所寫的內容完全相同,則應該使用循環來獲取每個公司,部門,BusinessArea,單位組的名稱和ID。

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 
+0

感謝弗洛林的迴應。我嘗試了你的解決方案,出於某種原因,我得到了「ORA-01489:字符串認知的結果太長了」。這是非常罕見的,因爲沒有一個全名超過4000個字符 – Jaanna

+0

,這是因爲有些單位包含太多客戶,所以(連接的)字符串變得很大。 –

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 
+0

產生與我的查詢大致相同的數據集,但是有兩個全表掃描。 –

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 | 
在你的用戶界面,你可以

然後通過成果循環,並顯示每件有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