日期:2014-05-17  浏览次数:20571 次

在MAX中筛选MAX,求好的SQL语句
id AreaNo AreaName ProvinceID PostCode ZoneCode Area AreaLevel AddWho AddDate
73 999999 北京直辖市 9 华北 一级市 13 2010-10-22 00:00:00.000
74 110100 北京市 8 华北 一级市 13 2010-10-22 00:00:00.000
75 110101 东城区 8 华北 NULL 13 2010-10-22 00:00:00.000
76 110102 西城区 9 华北 NULL 13 2010-10-22 00:00:00.000
76 110102 西城区 9 华北 NULL 14 2010-10-22 00:00:00.000
……

筛选出AddWho = 13 的ProvinceID最大的ID最小行?

------解决方案--------------------
SQL code
select id, AreaNo, AreaName, max(ProvinceID), PostCode ,ZoneCode, Area ,AreaLevel, AddWho
from tb
where AddWho=13 
group by

------解决方案--------------------
SQL code
----写错了忘记加分组条件了。
select id, AreaNo, AreaName, max(ProvinceID), PostCode ,ZoneCode, Area ,AreaLevel, AddWho
from tb
where AddWho=13 
group by id, AreaNo, AreaName, PostCode ,ZoneCode, Area ,AreaLevel, AddWho

------解决方案--------------------
SQL code
---也可以这样写
select id, AreaNo, AreaName, ProvinceID, PostCode ,ZoneCode, Area ,AreaLevel, AddWho
from tb
where AddWho=13 
group by id, AreaNo, AreaName, PostCode ,ZoneCode, Area ,AreaLevel, AddWho
having max(provinceID)

------解决方案--------------------
SQL code

select * from td a where AddWho=13 and
id=(select min(id) from td where AddWho=13 and ProvinceID=(select max(ProvinceID) from td where AddWho=13))