select * from table1 group by bh having sz=max(sz)
select bh,max(sz) from table1 group by bh order by bh
請問用group by 時 哪些函數可放在select 中 而group by 後不再需要該字段進行分組
create table tb(bh varchar(10),sz int) Insert into tb select 'M001',1 union all select 'M001',3 union all select 'M001',2 union all select 'M002',1 union all select 'M002',2 union all select 'M002',3 union all select 'M003',1 union all select 'M004',1 union all select 'M004',2 union all select 'M004',2select * from tbselect bh,sz=max(sz) from tb group by bh--結果 bh sz ----------------- M001 3 M002 3 M003 1 M004 2看來只有這種方法最好了
Insert into tb
select 'M001',1
union all select 'M001',3
union all select 'M001',2
union all select 'M002',1
union all select 'M002',2
union all select 'M002',3
union all select 'M003',1
union all select 'M004',1
union all select 'M004',2
union all select 'M004',2select * from tbselect bh,sz=max(sz) from tb group by bh--結果
bh sz
-----------------
M001 3
M002 3
M003 1
M004 2看來只有這種方法最好了