加入多个查询

时间:2013-08-05 11:56:02

标签: sql select union

我有一个很大的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               |
+-----+-----------------+

第二个QUERY应该计算每个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,但它们都不起作用。

任何帮助?

2 个答案:

答案 0 :(得分:1)

尝试使用Subselect,例如

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

答案 1 :(得分: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,而不是两个ID的2。或者我错过了一点?!?