【问题标题】:Join multiple queries加入多个查询
【发布时间】:2013-08-06 02:59:14
【问题描述】:

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

有什么帮助吗?

【问题讨论】:

  • 你遇到了什么错误?
  • 您的第二个查询不应该是SELECT id, SUM(value) FROM thebigtable WHERE (date LIKE '2010-08-01') GROUP BY id,即secondid分组吗?

标签: sql select union


【解决方案1】:

希望对你有帮助:

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。还是我错过了一点?!?

【讨论】:

    【解决方案2】:

    尝试使用子选择,例如

    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
    

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2018-05-09
      • 1970-01-01
      相关资源
      最近更新 更多