日期:2014-05-18  浏览次数:20467 次

一个常见得查询问题
有一表A的字段有ID(identity(1,1)),cellid,price,owedate
                  数据为   1,                                   101,     3000         2001-10-01
                                2,                                   101,     2500         2001-10-11
                                3,                                   102,     2800         2001-11-01
                                .........
现在想根据cellid   进行分组查询,得出owedate日期最新的一条记录的SQL语句该怎么写?
比如得出结果为
                              id                                   cellid             price             owedate
                                2                                         101                   2500                 2001-10-   11
                                3                                         102                   2800                 2001-11-01
                              ........
  不知说清楚了没有!     thinks



------解决方案--------------------
最容易理解的一种
select * from A where owedate in (select max(owedate) owedate from A group by cellid)
------解决方案--------------------
select a.* from A a,
(select ID=max(ID),owedate=max(owedate) from A group by cellid) b
where a.ID=b.ID