尝试根据另一个参数获取数据统计信息



基于其他数据集,在数据上再次努力。

我有一个客户列表。喜欢以下内容:

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

您可以使用existsin选择日期,然后计算平均值:

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

最新更新