我希望能够找出每月的平均值
目前我的代码是
fred = String.Format("{0}{1}{2}{1}{0}", LeftBrkt, RghtBrkt, fred);
我基本上需要它
SELECT
company,
COUNT(company) AS 'count'
FROM Information
GROUP BY company
我希望结果看起来像这样
SELECT company,
count(company) as 'count'
avg(count(company)) per month as 'average'
FROM Information
group by company
答案 0 :(得分:2)
一种非常简单的方法是先按公司和月计算,然后汇总这些数据,以获得每家公司的总数和平均值。
select
company,
sum(cnt) as records,
avg(cnt) as records_per_month
from
(
select company, year(start_date), month(start_date), count(*) as cnt
from information
group by company, year(start_date), month(start_date)
) agg
group by company;
但请阅读我对你问题的评论。
答案 1 :(得分:0)
SELECT YEAR(yourDate) * 100 + MONTH(yourDate) YYMM,
company,
count(company) as 'count'
avg(count(company)) per month as 'average'
FROM Information
group by company
,YEAR(yourDate) * 100 + MONTH(yourDate)