2014-04-04 65 views
0

我正在嘗試構建一個Oracle SQL查詢,當計數大於1時,這會使我按行組織的行與組成的行一起分組。示例和執行分組的SQL查詢。任何幫助或建議將不勝感激。Oracle SQL - 組和細節記錄

例如使用下面的數據集 -

====================== 
ID | NAME | AUTHOR 
====================== 
2 | Abc | John 
6 | Abc | John 
3 | Xyz | Mike 
4 | Abc | Mike 
5 | Xyz | John 
1 | Abc | Mike 
7 | PQR | Raj 


Expected Result - 
=========================== 
ID | NAME | AUTHOR | COUNT 
=========================== 
    | Abc |  | 4 
2 | Abc | John |  
6 | Abc | John |  
4 | Abc | Mike |  
1 | Abc | Mike |  
    | PQR |  | 1 
    | Xyz |  | 2 
3 | Xyz | Mike |  
5 | Xyz | John |  


SELECT NAME, COUNT(NAME) from (
SELECT 2 as ID, ' Abc ' as NAME, ' John ' as AUTHOR FROM DUAL 
UNION 
SELECT 6 as ID, ' Abc ' as NAME, ' John ' as AUTHOR FROM DUAL 
UNION 
SELECT 3 as ID, ' Xyz ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
UNION 
SELECT 4 as ID, ' Abc ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
UNION 
SELECT 5 as ID, ' Xyz ' as NAME, ' John ' as AUTHOR FROM DUAL 
UNION 
SELECT 1 as ID, ' Abc ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
UNION 
SELECT 7 as ID, ' PQR ' as NAME, ' Raj ' as AUTHOR FROM DUAL) 
GROUP BY NAME 
ORDER by NAME; 
+0

對不起,我不明白,把那些在一起。爲什麼選擇固定數據(比如2作爲ID,'abc'作爲名稱等)從表格字段的表格中選擇?爲什麼聯盟?一個查詢返回不同的名稱和COUNT很容易完成,我可以幫你,但其餘的我不忍。請澄清。 – ericpap

回答

1
SQL> with t as (
    2 SELECT 2 as ID, ' Abc ' as NAME, ' John ' as AUTHOR FROM DUAL 
    3 UNION 
    4 SELECT 6 as ID, ' Abc ' as NAME, ' John ' as AUTHOR FROM DUAL 
    5 UNION 
    6 SELECT 3 as ID, ' Xyz ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
    7 UNION 
    8 SELECT 4 as ID, ' Abc ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
    9 UNION 
10 SELECT 5 as ID, ' Xyz ' as NAME, ' John ' as AUTHOR FROM DUAL 
11 UNION 
12 SELECT 1 as ID, ' Abc ' as NAME, ' Mike ' as AUTHOR FROM DUAL 
13 UNION 
14 SELECT 7 as ID, ' PQR ' as NAME, ' Raj ' as AUTHOR FROM DUAL) 
15 select id, name, author, count# 
16 from (
17 select t.id, t.name, t.author, decode(grouping(id),1,count(*),null) count#, 
18 count(*) over (partition by name) cn, grouping(id) gid 
19 from t 
20 group by grouping sets((id,name,author),(name)) 
21 ) 
22 where (cn != 2 or count# is not null) 
23 order by name, gid desc, author 
24/

     ID NAME AUTHOR  COUNT#            
---------- ------ ------ ----------            
      Abc     4            
     2 Abc John               
     6 Abc John               
     4 Abc Mike               
     1 Abc Mike               
      PQR     1            
      Xyz     2            
     5 Xyz John               
     3 Xyz Mike     
0
select 
    id, name, author, decode(grouping_id(id, name), 2, count(*)) count 
from 
    books 
group by 
    rollup(name, (author, id)) 
having 
    grouping_id(id, name) != 3 
order 
    by name, id nulls first 
0

的名字與作者的計數分組的數據:

SELECT NULL id, name, NULL author, count(author) "count" 
FROM myTable 
GROUP BY name 

加時,有更多的則是細節一位作者:

SELECT id, name, author, NULL "count" 
FROM mytable 
WHERE name NOT IN (SELECT name FROM myTable GROUP BY name HAVING count(1) = 1) 

少許爲了得到想要的結果

SELECT id, name, author, NULL "count" 
FROM mytable 
WHERE name NOT IN (SELECT name FROM myTable GROUP BY name HAVING count(1) = 1) 
UNION ALL 
SELECT NULL id, name, NULL author, count(author) "count" 
FROM myTable 
GROUP BY name 
ORDER BY 2, 4, 1, 3 
+0

你正在做多個表掃描,所以我相信這是最佳的。 – user3498646