0

给定伪表:

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

或者

result=AVG(A)+AVG(B)

按日期分组?

4

2 回答 2

1

它应该计算结果

这是关于SqlFiddle的演示。

select (AVG(case when tag = 'A' then read end) + AVG(case when tag = 'B' then read end)) 'result', data
from TBL
group by data
于 2013-10-22T16:58:50.940 回答
0

您可以为每个标签单独选择一个总和或平均值,然后在每个单独的查询中使用您想要的操作进行选择

select (select SUM([read]) from table where tag = 'A') + 
  (select SUM([read]) from table where tag = 'B') 
于 2013-10-22T17:33:52.033 回答