试图根据另一个参数获取数据的统计信息
问题描述:
再次根据其他数据集对数据进行统计。试图根据另一个参数获取数据的统计信息
我有一个客户名单。像下面这样:
CustomerID Value Date
1 3 01/01/2017
2 2 01/02/2017
3 1 01/02/2017
1 5 01/04/2017
1 6 01/04/2017
2 1 01/04/2017
2 2 01/04/2017
我想获得一个平均日期范围的天凡客2还具有价值的客户1。有人有想法吗?
例如
Select avg(value)
from Table where customerid=1
and (customer 2 values are not blank)
and date between '01/01/2017' and '01/31/2017'
我使用SQL Server Express 2012
答
另一种选择
Select AvgValue = Avg(Value+0.0) -- Remove +0.0 if you want an INT
From YourTable
Where CustomerID = 1
and Date in (Select Distinct Date from YourTable Where CustomerID=2)
返回
AvgValue
5.500000
答
可以使用exists
或in
选择的日期,然后计算平均值:如果你想
select avg(value)
from datatbl t
where customerid = 1 and
exists (select 1 from datatbl t2 where t2.customerId = 2 and t2.date = t.date);
平均为,日期为,则包括group by date
。
你想一个平均的所有天或每天一个平均? –