我对SQL很陌生,并且无法弄清楚如何找到一些平均值。基本上我有3列的表,日期,ID和值。我试图每天查找每个ID的平均值。实际的表有数千个条目,每天为每个ID记录一个变量值。下面的样本表 -
SAMPLE TABLE
Date ID Value
1-1-14 12:45 1 5
1-1-14 13:45 1 6
1-1-14 08:45 2 8
1-1-14 10:45 2 8
1-2-14 01:45 1 2
1-2-14 04:45 1 4
1-2-14 01:45 2 20
1-2-14 04:45 2 24
SAMPLE RESULTS
Date ID AvgValue
1-1-14 1 5.5
1-1-14 2 8
1-2-14 1 3
1-2-14 2 22
我非常感谢任何帮助!谢谢!
答案 0 :(得分:2)
基本查询很简单:
select date, id, avg(value)
from your_table
group by date, id, avg
但是,由于您只需要日期时间列的日期部分,您可以将其转换/转换为更窄的日期类型,而且,如果您要平均的值是int,则可能需要将其转换为首先浮点类型。对于SQL Server,查询可能如下所示:
select
cast(date as date) as date,
id,
avg(cast(value as decimal(10,5))) as avg
from table1
group by cast(date as date), id
order by 1