第1个回答 推荐于2020-12-21
select sum(收入)/(select count(*) from (select distinct month(日期) from 收支表 where 收入>0 and year(日期)=year(now()))),
sum(支出)/(select count(*) from (select distinct month(日期) from 收支表 where 支出>0 and year(日期)=year(now()))) from 收支表 where year(日期)=year(now())
我在acces里边测试过了,肯定能用,不能用我给你分
以上,希望对你有所帮助!本回答被提问者采纳
第2个回答 2009-08-26
通过substr()函数把日期字段中的年月提取出来,然后分组,求平均值
select substr(日期,1,6) as 年月,
avg(收入) as 平均收入,
avg(支出) as 平均支出
from 收支表
group by substr(日期,1,6);
得到的结果集类似于:
年月 平均收入 平均支出
2009-5 300 200
2009-6 400 75
第3个回答 2009-08-26
select cast(year(日期) as char(4))+'-'+cast(month(日期) as varchar(2)) 月份,sum(收入)/count(*),sum(支出)/count(*)
from 收支表
group by cast(year(日期) as char(4))+'-'+cast(month(日期) as varchar(2))
第4个回答 2009-08-26
select '2009' 年份,
convert(decimal(10,0),SUM(收入)/SUM(case when 收入>0 then 1 else 0 end)) 平均收入,
convert(decimal(10,0),SUM(支出)/SUM(case when 支出>0 then 1 else 0 end)) 平均支出
from 收支表
where year(日期)=2009