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

max()group by的应用问题,求解。
如果有t1(year,company,tax)
--------------
2010 comp1 100
2011 comp1 200
2011 comp2 100
2012 comp2 300

需要的结果
公司最新一年的年份,公司名,纳税金额
2011 comp1 200
2012 comp2 300

应该怎样写查询语句?

------解决方案--------------------
SQL code
select * from t1 t
where not exists(select 1 from t1 where company=t.company and [year]>t.[year])

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

select [year],company,tax from t1
    where tax=(select max(tax) from t1 t2 where t2.company=t1.company)
order by t1.company