mysql - sql 请求最大分组依据

标签 mysql sql-server

大家好,你们能帮我吗? 请在下面的图像中找到输入和输出数据 因此,我需要编写sql请求以仅显示每个competent_question_id中的最大日期,并且还显示具有该日期的competent_answer_id的值(我也按response_id分组,但我将其隐藏在此处)

提前谢谢

SELECT response_id, competence_question_id, competence_answer_id, MAX(date_created)
FROM competence_vote_history
WHERE response_id = 53178
GROUP BY response_id, competence_question_id, competence_answer_id
ORDER BY response_id, competence_question_id

the table one with input data

the table two with output data

最佳答案

尝试以下查询以获得所需的结果。

SELECT competence_question_id, competence_answer_id, date_created, answer_value
FROM competence_vote_history i
WHERE response_id = 53178 
    AND date_created = (
        SELECT MAX(date_created) 
        FROM competence_vote_history 
        WHERE response_id = i.response_id 
        AND competence_question_id = i.competence_question_id)
ORDER BY response_id, competence_question_id

关于mysql - sql 请求最大分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513538/

相关文章:

php、jquery 和 ajax - 根据先前选择的值填充选择下拉列表

c# - 为什么 asp.net 使用 GUID 列而不是 bigint

sql-server - 为什么通过在 SQL 查询中执行所描述的修改可以显着提高性能?

sql-server - SQL 服务器 2008 : Null Return in Dynamic XML Query

通过 VS 2005 将 excel 电子表格上传到 SQL 数据库时的 C# 用户界面

mysql - 在文本文件中逐行运行作为 mySQL 查询(cmd)

Mysql查询,比较表结果

php - MySQL SQL语法错误;查看与您的 MySQL 服务器版本相对应的手册,了解要使用的正确语法

sql - 如何编写 SQL 查询来操作 2 个表之间的数据?

php - 如果另一个字段等于 5 或​​ 10,则不要乘以一个字段