SQL SELECT SUM来自两个表和GROUP BY日期

时间:2014-02-04 16:47:07

标签: sql join group-by sum subquery

我有两个表(rescuescounts),在MySQL 5.1数据库中有一对多的关系。 rescues中可能有多个行具有相同的日期时间。在counts中,至少有一行(更多)与rescues相关联(rescues.id = counts.id_rescue)。

我想得到:

  • “救援”栏目的总和
  • “计数”中某些列的总和
  • “计数”中列的总和
  • 按日期对结果进行分组

这就是我正在做的事:http://sqlfiddle.com/#!8/9a290/2/0

我收到了这些结果(错误的sum_volounteers,但更正了sum_alivesum_dead):

date        sum_volounteers   sum_alive   sum_dead
2014-02-02  5                 138         18
2014-02-04  4                 53          15

我期待的是:

date        sum_volounteers   sum_alive   sum_dead
2014-02-02  11                138         18
2014-02-04  5                 53          15

3 个答案:

答案 0 :(得分:1)

我相信你只需SUM()志愿者进行外部查询:

SELECT
r.date,
sum(r.sum_volounteers) AS sum_Volunteers,
sum(c.males + c.females + c.uncertains + c.couples*2) AS sum_alive,
sum(c.roadkills) AS sum_dead
FROM counts AS c
JOIN(
  SELECT
  r2.id AS selected_id,
  sum(r2.volounteers) AS sum_volounteers,
  CAST(r2.start AS DATE) AS date
  FROM rescues AS r2
  GROUP BY selected_id
) AS r ON c.id_rescue = r.selected_id
WHERE r.date >= '2014-02-02' AND r.date <= '2014-02-04'
GROUP BY r.date

演示:SQL Fiddle

由于您只对该日期进行分组,因此MySQL只选择了一个sum_volunteers值从您的子查询返回,而该日期有多个。

答案 1 :(得分:0)

编辑:我发现使用此查询时出现问题,如果rescues中有两行以上的行rescues.volounteers中的行号相同,则结果错误。请参阅我的其他答案以获得正确的解决方案。

<击> 感谢其他评论,我发现外部查询需要SUM(DISTINCT r.volounteers)http://sqlfiddle.com/#!8/9a290/69/0

不需要在子查询中使用SUM()

SELECT
r.date,
sum(DISTINCT r.volounteers) AS sum_volounteers,
sum(c.males + c.females + c.uncertains + c.couples*2) AS sum_alive,
sum(c.roadkills) AS sum_dead
FROM counts AS c
JOIN(
  SELECT
  r2.id AS selected_id,
  r2.volounteers AS volounteers,
  CAST(r2.start AS DATE) AS date
  FROM rescues AS r2
  GROUP BY selected_id
) AS r ON c.id_rescue = r.selected_id
WHERE r.date >= '2014-02-02' AND r.date <= '2014-02-04'
GROUP BY r.date

现在我得到了正确的结果:

date        sum_volounteers   sum_alive   sum_dead
2014-02-02  11                138         18
2014-02-04  5                 53          15

<击>

答案 2 :(得分:0)

解决方案是在外部查询中选择SUM(r.volounteers) AS sum_volounteers,然后在子查询中获取sum_alivesum_deaddate_of_sums(使用JOIN子查询)和GROUP BY date_of_sums然后JOIN c.date_of_sums = CAST(r.start AS DATE)

我在rescues中添加的记录在rescues.volounteers列中的编号相同时发布的其他查询失败,这是正确的:

SELECT
   r.id,
   CAST(r.start AS DATE) AS date,
   SUM(r.volounteers) AS sum_volounteers,
   sum_alive,
   sum_dead
FROM rescues AS r
JOIN(
   SELECT
   CAST(r2.start AS DATE) AS date_of_sums,
   sum(c2.males+c2.females+c2.uncertains+c2.couples*2) AS sum_alive,
   sum(roadkills) AS sum_dead
   FROM counts AS c2
   JOIN rescues as r2
   ON r2.id = c2.id_rescue
   GROUP BY date_of_sums
  ) AS c
ON c.date_of_sums = CAST(r.start AS DATE)
WHERE
   CAST(r.start AS DATE) >= '2014-02-02'
   AND CAST(r.start AS DATE) <= '2014-02-04'
GROUP BY CAST(r.start AS DATE)

这是一个演示:

此查询提供正确的结果:

date        sum_volounteers   sum_alive   sum_dead
2014-02-02  11                138         18
2014-02-04  9                 73          23