我正在尝试创建每个学生每天最高分的摘要.
示例数据:
???????????????????????????????????????????????
? id ? student_id ? score ? date_time ?
???????????????????????????????????????????????
? 1 ? 1 ? 5 ? 2018-07-01 9:30 ?
? 2 ? 1 ? 3 ? 2018-07-01 15:30 ?
? 3 ? 1 ? 7 ? 2018-07-02 8:30 ?
? 4 ? 2 ? 7 ? 2018-07-01 9:30 ?
? 5 ? 2 ? 8 ? 2018-07-01 15:30 ?
? 6 ? 2 ? 8 ? 2018-07-02 8:30 ?
? 7 ? 3 ? 4 ? 2018-07-02 10:30 ?
? 8 ? 3 ? 10 ? 2018-07-02 13:45 ?
???????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
期望的结果:
??????????????????????????????????????????????????????????????????????????????
? student_id ? date ? score_total ? best_score ? best_score_date_time ?
??????????????????????????????????????????????????????????????????????????????
? 1 ? 2018-07-01 ? 8 ? 5 ? 2018-07-01 9:30 ?
? 1 ? 2018-07-02 ? 7 ? 7 ? 2018-07-02 8:30 ?
? 2 ? 2018-07-01 ? 15 ? 8 ? 2018-07-01 15:30 ?
? 2 ? 2018-07-02 ? 8 ? 8 ? 2018-07-02 8:30 ?
? 3 ? 2018-07-02 ? 14 ? 10 ? 2018-07-02 13:45 ?
??????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止(和我的问题):
SELECT
student_id,
date_time::date as date,
SUM(score) as score_total,
MAX(score) as best_score,
date_time as best_score_date_time -- << HOW TO GET THIS ONE?
FROM
score
GROUP BY
student_id,
date_time::date
Run Code Online (Sandbox Code Playgroud)
小智 5
以下是简单的解决方案.
更换
date_time as best_score_date_time
同
( array_agg(date_time order by score desc) )[1]
| 归档时间: |
|
| 查看次数: |
44 次 |
| 最近记录: |