How to compare the value of two rows with SQL?

烈酒焚心 提交于 2019-12-04 19:13:18

This can be achieved by a self (anti) join:

SELECT a.*
FROM   (SELECT name, dt, SUM(score) as sum_score 
        FROM   performance 
        GROUP BY name, dt) a
JOIN   (SELECT name, dt, SUM(score) as sum_score
        FROM   performance 
        GROUP BY name, dt) b
ON     a.name = b.name AND a.sum_score = b.sum_score AND a.dt < b.dt
select dt, name, sum(score), dt 
from performance 
group by name, dt
having count(*) > count(distinct score);

*Note: If you are grouping by dt, name, I think you should display them also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!