0

我有一个大的 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,但它们都不起作用。

有什么帮助吗?

4

2 回答 2

1

尝试使用子选择,例如

SELECT a.id, b.secondid, c.value_sum 
FROM
    thebigtable a,
    ( SELECT id, COUNT( secondid ) as sec 
      FROM thebigtable ) b,
    ( SELECT id, SUM( value ) as value_sum 
      FROM thebigtable 
      GROUP BY secondid ) c,
WHERE a.id = b.id
AND   a.id = c.id
于 2013-08-05T12:21:48.143 回答
0

我希望这能帮到您:

SELECT id, SUM(cnt) cnt, SUM(IF(yemo='1008',val,0)) val FROM (
 SELECT id,
        date_format(`date`,'%y%m')  yemo, 
        COUNT(secondid)             cnt,
        SUM(`value`)                val
 FROM bigtable GROUP BY id,date_format(`date`,'%y%m')
) t WHERE yemo between '1008' AND '1009' GROUP BY id

虽然,根据我的SQLfiddle ,第一个查询中列的结果数count(secondid)应该是 4 而不是 2 对于有问题的两个 id。还是我错过了一点?!?

于 2013-08-05T12:16:47.760 回答