获取每组中的最后一条记录并对其中一些进行求和

发布于 2024-10-17 05:38:25 字数 972 浏览 5 评论 0原文

我对 mysql 的 sql 查询有问题,要获取每个组中的最后一条记录并对一个查询中的某些字段求和。我有一张表:

name        date    interested  made_call
andrew.h    2011-02-04      10        10
andrew.h    2011-02-11      20        10
andrew.h    2011-02-13      2         10
sasha.g  2011-02-11      5         20
sasha.g  2011-02-12      5         1

我需要按名称对 made_call 列分组求和并返回感兴趣的最后一条记录。 这是我想要得到的结果:

name        date    interested  made_call
andrew.h    2011-02-13      2         30
sasha.g  2011-02-12      5         21

我尝试通过此查询获取结果

SELECT a.name,a.date,a.interested,sum(made_call) as made_call
FROM `resultboard` a 
WHERE a.attendence = 1 
AND NOT EXISTS (select 1 from resultboard where name = a.name
       and id > a.id and attendence = 1)
GROUP BY name

,但在结果中我得到的结果

  andrew.h  2011-02-13      2         10
  sasha.g    2011-02-12      5         1

是查询没有求和,只是返回组中的最后一条记录 帮助)

i have a problem with sql query to mysql to take the last record in each group and sum some field in one query.i have a table:

name        date    interested  made_call
andrew.h    2011-02-04      10        10
andrew.h    2011-02-11      20        10
andrew.h    2011-02-13      2         10
sasha.g  2011-02-11      5         20
sasha.g  2011-02-12      5         1

i need to sum made_call column grouping by name and return the last record from interested.
here what i want to get in result:

name        date    interested  made_call
andrew.h    2011-02-13      2         30
sasha.g  2011-02-12      5         21

i tried to get result with this query

SELECT a.name,a.date,a.interested,sum(made_call) as made_call
FROM `resultboard` a 
WHERE a.attendence = 1 
AND NOT EXISTS (select 1 from resultboard where name = a.name
       and id > a.id and attendence = 1)
GROUP BY name

but in result i got

  andrew.h  2011-02-13      2         10
  sasha.g    2011-02-12      5         1

so the query didnot sum, just return the last record from group
help)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2024-10-24 05:38:25

如果表很大,这可能会有点慢,但它会得到想要的结果:

SELECT a.name, t.date, a.interested, t.calls
FROM resultboard a 
JOIN (SELECT name, MAX(date) AS date, SUM(made_call) AS calls FROM resultboard 
     GROUP BY name) AS t
     ON a.name = t.name AND a.date = t.date

That may be a little slow if the table is very big, but it will get the wanted result:

SELECT a.name, t.date, a.interested, t.calls
FROM resultboard a 
JOIN (SELECT name, MAX(date) AS date, SUM(made_call) AS calls FROM resultboard 
     GROUP BY name) AS t
     ON a.name = t.name AND a.date = t.date
善良天后 2024-10-24 05:38:25

您的 WHERE 子句将除最后一行之外的所有行作为总和的一部分排除在外。

在其他一些数据库中,您可以使用 LAST 聚合函数。 MySQL没有这个,但是你可以 对于您的情况,像这样模拟它:

SELECT
    a.name,
    SUBSTRING_INDEX(
      GROUP_CONCAT(CAST(a.date AS CHAR) ORDER BY date desc),
      ',', 1
    ) AS date,
    SUBSTRING_INDEX(
      GROUP_CONCAT(CAST(a.interested AS CHAR) ORDER BY date desc),
      ',', 1
    ) AS interested,
    sum(made_call) as made_call
FROM `resultboard` a 
WHERE a.attendence = 1 
GROUP BY name

它在大型数据集上可能不会很快,但如果我的研究是正确的,它至少应该可以完成工作。我还没有测试过这个,所以 YMMV。

Your WHERE clause is eliminating all but the last row from consideration as part of the sum.

In some other DB's you could use the LAST aggregate function. MySQL doesn't have that, but you can emulate it like so for your case:

SELECT
    a.name,
    SUBSTRING_INDEX(
      GROUP_CONCAT(CAST(a.date AS CHAR) ORDER BY date desc),
      ',', 1
    ) AS date,
    SUBSTRING_INDEX(
      GROUP_CONCAT(CAST(a.interested AS CHAR) ORDER BY date desc),
      ',', 1
    ) AS interested,
    sum(made_call) as made_call
FROM `resultboard` a 
WHERE a.attendence = 1 
GROUP BY name

It might not be fast on large data sets, but it should at least do the job if my research is correct. I haven't tested this, so YMMV.

绅士风度i 2024-10-24 05:38:25

我认为使用 WITH ROLLUP 作为 GROUP BY 修饰符可能会对您有所帮助。 http://dev.mysql.com/doc/ refman/5.0/en/group-by-modifiers.html

编辑;我错了,不需要WITH ROLLUP

SELECT r.name,  
       MAX(r.date) as date,  
       (SELECT r2.interested FROM resultboard r2 WHERE r2.name = r.name ORDER BY r.date DESC LIMIT 1),  
       SUM(made_call) as made_call  
FROM resultboard r 
GROUP BY name;

I think that using WITH ROLLUP for GROUP BY modifier may help you. http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

Edit; I got i wrong , no need for WITH ROLLUP

SELECT r.name,  
       MAX(r.date) as date,  
       (SELECT r2.interested FROM resultboard r2 WHERE r2.name = r.name ORDER BY r.date DESC LIMIT 1),  
       SUM(made_call) as made_call  
FROM resultboard r 
GROUP BY name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文