對於sql server來說:
group by和order by同時存在的情況是,order by對group by後的結果再進行排序的,所以order by後面的排序字段需要在select裡出現的,即order
by
子句中的列必須包含在聚合函式或
group
by
子句中。
如:
下面這個就是錯的
sql code?
1
select
[col1] ,[col2]
from
[tb]
group
by
[col1] ,[col2]
order
by
[col1] ,[col2] ,[col3]
而下面的都是對的
sql code?
1
2
3
select
[col1] ,[col2],
max
([col3])
from
[tb]
group
by
[col1] ,[col2]
order
by
[col1] ,[col2] ,
max
([col3])
select
[col1] ,[col2],
max
([col3])
as
[col3]
from
[tb]
group
by
[col1] ,[col2]
order
by
[col1] ,[col2] ,[col3]
select
[col1] ,[col2]
from
[tb]
group
by
[col1] ,[col2] ,[col3]
order
by
[col1] ,[col2] ,[col3]
但是mysql沒有上面的要求,如select * from account_withdraw_records group by status可以正常執行。
先ORDER BY再GROUP BY問題
我需要實現標識最小的顯示出來,並且binglihao沒有重複 以張三00為例,張三00有兩條資料,一條標識是50,另一條是56 語句 select from select h.xingming as xingming,h.binglihao as binglihao,j.biaoshi as bia...
group by與order by同時使用
orderby子句中的列必須包含在聚合函式或groupby子句中。eg sql view plain copy select col1 col2 max col3 from tb group by col1 col2 order by col1 col2 max col3 select col1 c...
group by 和 聚合函式
01.from test 該句執行後,應該結果和表1一樣,就是原來的表。02.from test group by name 該句執行後,我們想象生成了虛擬表3,如下所圖所示,生成過程是這樣的 group by name,那麼找name那一列,具有相同name值的行,合併成一行,如對於name值為a...