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

问题描述:

再次根据其他数据集对数据进行统计。试图根据另一个参数获取数据的统计信息

我有一个客户名单。像下面这样:

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

+0

你想一个平均的所有天或每天一个平均? –

另一种选择

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

+0

感谢您的快速回复!我很抱歉,但t和t2令我困惑。如果我的表的名称是 “Datatbl” 我会写它想: 从datatbl 选择AVG(值) 其中客户id = 1和 存在(从datatbl datatbl2其中datatbl2.customerID = 2 和datatbl2.date = datatbl选择1 .date 对不起,我知道这是一个天真的问题... – Shnozz

+0

让我换个话题,如果我的表名是Datatbl,我的查询应该是什么样子?我假设't'和't2'是引用数据我应该知道,但我没有得到它... – Shnozz