读书人

max()group by的应用有关问题求解

发布时间: 2012-08-16 12:02:15 作者: rapoo

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 twhere 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 

读书人网 >SQL Server

热点推荐