加入结果并执行算术平均值

时间:2013-08-27 09:59:17

标签: mysql sql mean

有两个表:“saldos_one”和“saldos_two”,两个表都有以下字段:CLIENTID,DATE,VALUE。

我必须在某些时间段从每个CLIENTID的算术平均值得到TOTAL算术平均值。

让我们举一些例子:

> SELECT * FROM saldos_one;
+----------+------------+---------+
| CLIENTID | DATE       | VALUE   |
+----------+------------+---------+
| 1        | 2009-08-01 | 1000.00 |
| 1        | 2009-09-01 | 2000.00 |
| 1        | 2009-10-01 | 3000.00 |
| 2        | 2009-08-01 | 1000.00 |
| 2        | 2009-09-01 | 2000.00 |
| 2        | 2009-10-01 | 3000.00 |
| 3        | 2009-08-01 | 1000.00 |
| 3        | 2009-09-01 | 2000.00 |
| 3        | 2009-10-01 | 3000.00 |
| 4        | 2009-08-01 | 1000.00 |
| 4        | 2009-09-01 | 2000.00 |
| 4        | 2009-10-01 | 3000.00 |
+----------+------------+---------+

> SELECT * FROM saldos_two;
+----------+------------+---------+
| CLIENTID | DATE       | VALUE   |
+----------+------------+---------+
| 1        | 2009-08-01 | 10.00   |
| 1        | 2009-09-01 | 20.00   |
| 1        | 2009-10-01 | 30.00   |
| 2        | 2009-08-01 | 100.00  |
| 2        | 2009-09-01 | 200.00  |
| 2        | 2009-10-01 | 300.00  |
| 3        | 2009-08-01 | 1000.00 |
| 3        | 2009-09-01 | 2000.00 |
| 3        | 2009-10-01 | 3000.00 |
| 5        | 2009-08-01 | 1.00    |
| 5        | 2009-09-01 | 2.00    |
| 5        | 2009-10-01 | 3.00    |
+----------+------------+---------+

对每个表进行QUERY算术后:

> SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2) 
FROM saldos_one 
WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
GROUP BY CLIENTID;
+----------+---------+
| CLIENTID | VALUE   |
+----------+---------+
| 1        | 2000.00 |
| 2        | 2000.00 |
| 3        | 2000.00 |
| 4        | 2000.00 |
+----------+---------+

> SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2) 
FROM saldos_two
WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
GROUP BY CLIENTID;
+----------+---------+
| CLIENTID | VALUE   |
+----------+---------+
| 1        | 20.00   |
| 2        | 200.00  |
| 3        | 2000.00 |
| 5        | 2.00    |
+----------+---------+

我想得到的是来自不同表的算术平均值的每个客户的算术方法,即:

+----------+---------+
| CLIENTID | VALUE   |
+----------+---------+
| 1        | 1010.00 | = 2000.00 + 20.00 / 2
| 2        | 200.00  | = 200.00 + 200.00 / 2
| 3        | 2000.00 | = 2000.00 + 2000.00 / 2
| 4        | 1000.00 | = 2000.00 + 0 / 2
| 5        | 1.00    | = 2.00 + 0 / 2
+----------+---------+

解决方案:请参阅@ bvr的回复

4 个答案:

答案 0 :(得分:3)

试试这个

SELECT CLIENTID,SUM(VALUE)/2 VALUE FROM
(
    SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2) VALUE 
    FROM saldos_one 
    WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
    GROUP BY CLIENTID;
    UNION ALL
    SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2) VALUE
    FROM saldos_two
    WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
) t
 GROUP BY CLIENTID

答案 1 :(得分:1)

您可以使用UNION个表格,然后对它们执行分析。

E.g。

SELECT CLIENTID, AVG(VALUE) FROM (
    SELECT CLIENTID, VALUE FROM saldos_one WHERE 
       (DATE BETWEEN '2009-08-01' AND '2009-10-01')
  UNION
    SELECT CLIENTID, VALUE FROM saldos_two WHERE 
       (DATE BETWEEN '2009-08-01' AND '2009-10-01')
) t
GROUP BY CLIENTID

答案 2 :(得分:1)

select CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2)  from
(SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2)  as VALUE
FROM saldos_one 
WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
GROUP BY CLIENTID
union all
 SELECT CLIENTID, TRUNCATE(SUM(VALUE)/COUNT(VALUE), 2) 
FROM saldos_two
WHERE (DATE BETWEEN '2009-08-01' AND '2009-10-01')
GROUP BY CLIENTID) as A group by A.CLIENTID

答案 3 :(得分:0)

您可以使用AVG功能代替SUM(VALUE)/ COUNT(VALUE)。

关于问题的最后部分:

您可以在临时表中放入两个查询的结果,然后使用AVG函数将相同的查询应用于临时表,这样就可以在两个值(2000和20)之间应用平均值。

你桌子的所有行