Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 SQL将公式应用于平均值_Sql Server 2008 - Fatal编程技术网

Sql server 2008 SQL将公式应用于平均值

Sql server 2008 SQL将公式应用于平均值,sql-server-2008,Sql Server 2008,给定伪表: +-----+---------------------+------+ | tag | data | read | +-----+---------------------+------+ | A | 2013-10-10 15:00:00 | 1345 | +-----+---------------------+------+ | A | 2013-10-10 15:15:00 | 3454 | +-----+---------------

给定伪表:

+-----+---------------------+------+
| tag | data                | read |
+-----+---------------------+------+
| A   | 2013-10-10 15:00:00 | 1345 |
+-----+---------------------+------+
| A   | 2013-10-10 15:15:00 | 3454 |
+-----+---------------------+------+
| A   | 2013-10-10 15:30:00 | 2345 |
+-----+---------------------+------+
| A   | 2013-10-10 15:45:00 | 1132 |
+-----+---------------------+------+
| B   | 2013-10-10 15:00:00 | 6234 |
+-----+---------------------+------+
| B   | 2013-10-10 15:15:00 | 5432 |
+-----+---------------------+------+
| B   | 2013-10-10 15:30:00 | 4563 |
+-----+---------------------+------+
| B   | 2013-10-10 15:45:00 | 5432 |
+-----+---------------------+------+
是否可以仅使用SQL应用以下等式

例如:

result=AVG(A)-(AVG(B)+AVG(C))


按日期分组?

它应该计算结果

这里有一个演示


您可以分别为每个标记选择一个sum或avg,然后在每个查询上使用所需的操作进行选择

select (select SUM([read]) from table where tag = 'A') + 
  (select SUM([read]) from table where tag = 'B') 

它很好用。但必须删除“else 0”,因为它会影响平均值计算,所以
。。。平均值(标记为“A”然后读取结束时的情况).
select (AVG(case when tag = 'A' then read end) + AVG(case when tag = 'B' then read end)) 'result', data
from TBL
group by data
select (select SUM([read]) from table where tag = 'A') + 
  (select SUM([read]) from table where tag = 'B')