当前位置: 代码迷 >> Sql Server >> 在MAX中罗选MAX,求好的SQL语句
  详细解决方案

在MAX中罗选MAX,求好的SQL语句

热度:101   发布时间:2016-04-27 11:21:44.0
在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, AddWhofrom tbwhere AddWho=13 group by
------解决方案--------------------
SQL code
----写错了忘记加分组条件了。select id, AreaNo, AreaName, max(ProvinceID), PostCode ,ZoneCode, Area ,AreaLevel, AddWhofrom tbwhere AddWho=13 group by id, AreaNo, AreaName, PostCode ,ZoneCode, Area ,AreaLevel, AddWho
------解决方案--------------------
SQL code
---也可以这样写select id, AreaNo, AreaName, ProvinceID, PostCode ,ZoneCode, Area ,AreaLevel, AddWhofrom tbwhere AddWho=13 group by id, AreaNo, AreaName, PostCode ,ZoneCode, Area ,AreaLevel, AddWhohaving max(provinceID)
------解决方案--------------------
SQL code
select * from td a where AddWho=13 andid=(select min(id) from td where AddWho=13 and ProvinceID=(select max(ProvinceID) from td where AddWho=13))
  相关解决方案