按日期从两个SQL表中对数据进行分组

时间:2015-05-01 17:33:34

标签: mysql sql

考虑以下表格:

tbl1
------------

id   name   creationdate

0    AA     2015.05.11
1    BB     2015.04.27
2    cC     2015.04.18


tbl2
------------

id   name   creationdate

0    DD     2015.04.17
1    FF     2015.04.27
2    NN     2015.05.01
3    BV     2015.05.01

查询的正确语法是什么,可以按日期对两个表中的行数进行分组?结果是:

DATE         TBL1_COUNT   TBL2_COUNT

2015.04.17            0            1
2015.04.18            1            0
2015.04.27            1            1
2015.05.01            0            2
2015.05.11            1            0

到目前为止,我已设法通过以下方式从两个表中获取日期:

select  DISTINCT date(creationdate) from tbl1
union
select  DISTINCT date(creationdate) from tbl2

下一部分将从两个表中获取count(*)并按上述查询的结果对它们进行分组

4 个答案:

答案 0 :(得分:3)

  1. 您需要使用UNION ALL或联合的不同方面 消除重复,并对你的计数产生负面影响。
  2. 你需要一种方法来表示哪个值来自哪个表,所以我们添加一个列来区分,所以我为tbl1添加了1,为tbl2添加了2,并调用了列src。
  3. 然后我们在外部选择中添加一个case语句,在sum中添加一个sum以获得所需的计数,并在group by中聚合日期..
  4. 通过订单来消磨它,然后你得到你想要的东西。
  5. SELECT creationDate as Date, 
      sum(case when src=1 then 1 else 0 end) as tbl1_count, 
      sum(case when src=2 then 1 else 0 end) as tbl2_count
    FROM (
      SELECT creationdate, 1 as src
      FROM tbl1
    
      UNION ALL
    
      SELECT creationdate, 2 as src
      FROM tbl2) Sub
    GROUP BY CreationDate
    ORDER BY CreationDate
    

答案 1 :(得分:2)

你有个好的开始。如果我接近这个,我首先要从每个表中获取日期和计数,如下所示:

SELECT creationDate, COUNT(*) AS numRows
FROM t1
GROUP BY creationDate;

然后,您可以进行FULL OUTER加入以获得所有配对。 MySQL并没有内置完全连接,但你可以使用右连接和左连接的并集来模拟它,试试这个:

SELECT t1.creationDate, t1.numRows AS t1Rows, COALESCE(t2.numRows, 0) AS t2Rows
FROM(
  SELECT creationDate, COUNT(*) AS numRows
  FROM t1
  GROUP BY creationDate) t1
LEFT JOIN(
  SELECT creationDate, COUNT(*) AS numRows
  FROM t2
  GROUP BY creationDate) t2 ON t1.creationDate = t2.creationDate
UNION
SELECT t2.creationDate, COALESCE(t1.numRows, 0) AS t1Rows, t2.numRows AS t2Rows
FROM(
  SELECT creationDate, COUNT(*) AS numRows
  FROM t1
  GROUP BY creationDate) t1
RIGHT JOIN(
  SELECT creationDate, COUNT(*) AS numRows
  FROM t2
  GROUP BY creationDate) t2 ON t1.creationDate = t2.creationDate;

以下是SQL Fiddle示例。

答案 2 :(得分:1)

基本上,您可以模拟完整的外连接。 union distinct很好,实际上我们需要,我们必须确保包含足够的列,以使不同的选择能够准确满足我们的需求。

select d3, count(d1), count(d2)
from (
select t1.id id1, t2.id id2, t1.date as d1, t2.date as d2, coalesce(t1.date, t2.date) d3
  from tbl1 t1
    left join tbl2 t2
      on t1.date = t2.date
union 
select t1.id id1, t2.id id2, t1.date as d1, t2.date as d2, coalesce(t1.date, t2.date) d3
  from tbl1 t1
    right join tbl2 t2
      on t1.date = t2.date
) q
group by d3;

在这里演示:http://sqlfiddle.com/#!9/eaa3b/21

答案 3 :(得分:1)

我认为最有效的方法是有两个查询:一个计算table1中所有分组日期并将table2的计数设置为0,另一个计算table2中所有分组日期并将table1的计数设置为0,然后再使用UNION ALL查询组合它们,再次对组合并对结果计数求和:

SELECT creationDate, sum(count_t1), sum(count_t2)
FROM (
  SELECT creationDate, COUNT(*) AS count_t1, 0 AS count_t2
  FROM t1
  GROUP BY creationDate

  UNION ALL

  SELECT creationDate, 0 AS count_t1, COUNT(*) AS count_t2
  FROM t2
  GROUP BY creationDate
) s
GROUP BY creationDate