的exampe應該是不言自明:
SQL> create table testDistinct (col1, col2) as(
2 select 1, 100 from dual union all
3 select 1, 10 from dual union all
4 select 1, 10 from dual union all
5 select 2, 50 from dual union all
6 select 3, 1 from dual union all
7 select 3, 100 from dual
8 );
Table created.
SQL> select col1, avg(col2)
2 from testDistinct
3 group by col1;
COL1 AVG(COL2)
---------- ----------
1 40
2 50
3 50,5
SQL> select DISTINCT col1, avg(col2)
2 from testDistinct
3 group by col1;
COL1 AVG(COL2)
---------- ----------
1 40
2 50
3 50,5
應用本集團在DISTINCT的結果給出:
SQL> select col1, avg(col2)
2 from (
3 select DISTINCT col1, col2
4 from testDistinct
5 )
6 group by col1;
COL1 AVG(COL2)
---------- ----------
1 55
2 50
3 50,5
既然你做'GROUP BY col1','DISTINCT'是多餘的 - 結果已經不同了。 – jarlh
在jarlh的評論中展開:在SELECT之前處理GROUP BY。所以你不能改變這個例子,把它轉換成一個問題沒有爭議的問題。無論您如何重寫查詢,問題都是無關緊要的 - 首先處理GROUP BY,並且只有GROUP BY列可以包含在SELECT中(此時它們已經是DISTINCT)。 – mathguy
@jarlh當我在Col1上進行分組時,是不是隻按col1分組?所以在這種情況下,col2仍然可以有多個重複的權利?我的問題是,DISTINCT仍然會在SELECT上對WITHIN COL2中的重複項產生任何影響。感謝您的檢查! – maverick