我有一张类似
的表MyTable
periodEndDate TotTermLoans companyName
2009-09-30 NA ABB
2009-09-30 5.38 ABB
2009-09-30 4.34 ABB
2009-12-31 5.6 ABB
2009-12-31 5.6 ABB
2009-12-31 5.6 ABB
2010-03-31 5.47 ABB
2010-03-31 5.47 ABB
2010-03-31 5.0 ABB
我希望按年份分组并获得每年TotTermLoans
的平均值,但是根据我的查询,我会重复行:
SELECT YEAR(periodEndDate), AVG(TotTermLoans), companyName
FROM MyTable
GROUP BY YEAR(periodEndDate)
给出了
YEAR(periodEndDate) AVG(TotTermLoans) companyName
2009 5.304 ABB
2009 5.304 ABB
2009 5.304 ABB
2010 5.313 ABB
2010 5.313 ABB
2010 5.313 ABB
我想
YEAR(periodEndDate) AVG(TotTermLoans) companyName
2009 5.304 ABB
2010 5.313 ABB
我是否必须使用子查询?谢谢
答案 0 :(得分:3)
SELECT YEAR(periodEndDate), AVG(TotTermLoans), companyName
FROM MyTable
GROUP BY YEAR(periodEndDate), companyName
DISTINCT
关键字也可用于SELECT
语句,以删除重复的结果。