我有一个大的 MySQL DDBB,必须进行一些计算和聚合。其中一张表,比如说它叫做“thebigtable”,有各种信息,一些计算检索不同的信息。
让我们以“thebigtable”为例:
+-----+-----------+------------+---------+
| id | secondid | date | value |
+-----+-----------+------------+---------+
| 001 | AAA1 | 2010-01-01 | 1000.00 |
| 001 | AAA1 | 2010-02-01 | 1000.00 |
| 001 | AAA1 | 2010-03-01 | 1000.00 |
| 001 | AAA1 | 2010-04-01 | 1000.00 |
| 001 | AAA1 | 2010-05-01 | 1000.00 |
| 001 | AAA1 | 2010-06-01 | 1000.00 |
| 001 | AAA1 | 2010-07-01 | 1000.00 |
| 001 | AAA1 | 2010-08-01 | 1000.00 |
| 001 | AAA1 | 2010-09-01 | 1000.00 |
| 001 | AAA1 | 2010-10-01 | 1000.00 |
| 001 | AAA1 | 2010-11-01 | 1000.00 |
| 001 | AAA1 | 2010-12-01 | 1000.00 |
+-----+-----------+------------+---------+
| 001 | AAA2 | 2010-01-01 | 1000.00 |
| 001 | AAA2 | 2010-02-01 | 1000.00 |
| 001 | AAA2 | 2010-03-01 | 1000.00 |
| 001 | AAA2 | 2010-04-01 | 1000.00 |
| 001 | AAA2 | 2010-05-01 | 1000.00 |
| 001 | AAA2 | 2010-06-01 | 1000.00 |
| 001 | AAA2 | 2010-07-01 | 1000.00 |
| 001 | AAA2 | 2010-08-01 | 1000.00 |
| 001 | AAA2 | 2010-09-01 | 1000.00 |
| 001 | AAA2 | 2010-10-01 | 1000.00 |
| 001 | AAA2 | 2010-11-01 | 1000.00 |
| 001 | AAA2 | 2010-12-01 | 1000.00 |
+-----+-----------+------------+---------+
| 002 | AAA1 | 2010-01-01 | 1000.00 |
| 002 | AAA1 | 2010-02-01 | 1000.00 |
| 002 | AAA1 | 2010-03-01 | 1000.00 |
| 002 | AAA1 | 2010-04-01 | 1000.00 |
| 002 | AAA1 | 2010-05-01 | 1000.00 |
| 002 | AAA1 | 2010-06-01 | 1000.00 |
| 002 | AAA1 | 2010-07-01 | 1000.00 |
| 002 | AAA1 | 2010-08-01 | 1000.00 |
| 002 | AAA1 | 2010-09-01 | 1000.00 |
| 002 | AAA1 | 2010-10-01 | 1000.00 |
| 002 | AAA1 | 2010-11-01 | 1000.00 |
| 002 | AAA1 | 2010-12-01 | 1000.00 |
+-----+-----------+------------+---------+
| 002 | AAA2 | 2010-01-01 | 1000.00 |
| 002 | AAA2 | 2010-02-01 | 1000.00 |
| 002 | AAA2 | 2010-03-01 | 1000.00 |
| 002 | AAA2 | 2010-04-01 | 1000.00 |
| 002 | AAA2 | 2010-05-01 | 1000.00 |
| 002 | AAA2 | 2010-06-01 | 1000.00 |
| 002 | AAA2 | 2010-07-01 | 1000.00 |
| 002 | AAA2 | 2010-08-01 | 1000.00 |
| 002 | AAA2 | 2010-09-01 | 1000.00 |
| 002 | AAA2 | 2010-10-01 | 1000.00 |
| 002 | AAA2 | 2010-11-01 | 1000.00 |
| 002 | AAA2 | 2010-12-01 | 1000.00 |
+-----+-----------+------------+---------+
好的。现在让我们设置我必须执行的查询。第一个,计算每个 'id' 的不同 'secondid' 的数量,条件是日期是从 08 月到 10 月:
> SELECT id, COUNT(secondid) FROM thebigtable WHERE (date >= '2010-08-01') and (date <= '2010-10-01') GROUP BY id;
结果应该是:
+-----+-----------------+
| id | count(secondid) |
+-----+-----------------+
| 001 | 2 |
| 002 | 2 |
+-----+-----------------+
第二个查询,应该计算每个 id 的值的总和,条件是日期只是 08 月:
> SELECT id, SUM(value) FROM thebigtable WHERE (date LIKE '2010-08-01') GROUP BY secondid;
结果应该是:
+-----+-------------+
| id | sum(values) |
+-----+-------------+
| 001 | 2000.00 |
| 002 | 2000.00 |
+-----+-------------+
我想在一个结果下显示两个 SELECT ,即:
+-----+-----------------+-------------+
| id | count(secondid) | sum(values) |
+-----+-----------------+-------------+
| 001 | 2 | 2000.00 |
| 002 | 2 | 2000.00 |
+-----+-----------------+-------------+
我尝试创建视图并在之后加入,或者创建两个 SELECT 的 UNION,但它们都不起作用。
有什么帮助吗?