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