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')