简体   繁体   English

SQL:从组中获取最大日期的另一列

[英]SQL: From a group by get another column on max date

So I have a table more less like this name it tests_summary :所以我有一个表 more less like this name it tests_summary

test_id测试编号 completed完全的 remaining其余的 score_date得分日期
200 200 6 6个 1 1个 2022-05-02 2022-05-02
200 200 2 2个 7 7 2022-05-01 2022-05-01
200 200 5 5个 10 10 2022-04-29 2022-04-29
200 200 5 5个 15 15 2022-04-28 2022-04-28
199 199 10 10 0 0 2022-05-02 2022-05-02
199 199 2 2个 10 10 2022-05-01 2022-05-01

First I tried to group by each test_id, SUM every completed by each test, but I want to get in the remaining column, the remaining value in the most recent score date.首先,我尝试按每个 test_id 进行分组,对每个测试完成的 SUM 进行分组,但我想在剩余的列中获取最新分数日期中的剩余值。 Example test_id 200, the most recent date is 2022-05-02, and 1 is remaining that is the value I want.例如test_id 200,最近的日期是2022-05-02,剩下1就是我想要的值。 In this table is already ordered but in my real table is not.在这张表中已经订购,但在我的真实表中没有。
Expected result预期结果

test_id测试编号 completed完全的 remaining其余的
200 200 18 18 1 1个
199 199 12 12 0 0

version 5.7.12版本 5.7.12

You can use row number function to give you a 1 for every item you care about.您可以使用行号 function 为您关心的每个项目提供 1。

Note, I use * here as a shorthand but you should only select the columns you are interested in, in your final code.请注意,我在这里使用 * 作为简写,但在最终代码中,您应该只使用 select 您感兴趣的列。

SELECT * 
FROM (
  SELECT z.*, ROW_NUMBER(PARTITION BY test_id ORDER BY score_date DESC) AS RN
  FROM sometableyoudidnotname z
) X
WHERE X.RN = 1

You can use您可以使用

SELECT test_id, completed, remaining
  FROM (SELECT test_id,
               remaining,
               SUM(completed) OVER(PARTITION BY test_id) AS completed,               
               ROW_NUMBER() OVER(PARTITION BY test_id ORDER BY score_date DESC) AS rn
          FROM t) tt
 WHERE rn = 1

where you can get the latest date through descendingly sorted by the score_date while partitioning by test_id in order to group by each of them provided that your database and its version is suitable to use a window(or analytic) function如果您的数据库及其版本适合使用窗口(或分析),您可以通过按test_id降序排序同时按score_date分区以按每个日期分组来获取最新日期 function

Upd.更新。 That solution for PostgreSQL only.该解决方案仅适用于 PostgreSQL。 Thanks Hogan for correcting me感谢霍根纠正我

I'm not sure that my solution will be the most optimal for performance, but nevertheless it works.我不确定我的解决方案是否是性能最佳的,但它仍然有效。 The main idea is to use the DISTINCT ON construct on the "remaining" column and separately summarize the "completed" column with a subquery:主要思想是在“剩余”列上使用DISTINCT ON构造,并使用子查询单独汇总“已完成”列:

WITH
    t AS (SELECT 200                AS test_id,
                 0                  AS completed,
                 1                  AS remaining,
                 '2022-05-02'::DATE AS score_date
          UNION
          SELECT 200,
                 2,
                 3,
                 '2022-05-01'
          UNION
          SELECT 199,
                 10,
                 0,
                 '2022-05-02'
          UNION
          SELECT 199,
                 2,
                 10,
                 '2022-05-01')
SELECT
    distinct on (t1.test_id)
        t1.remaining,
        t1.test_id,
        (select sum(completed) from t where t.test_id = t1.test_id)
    FROM t t1
ORDER BY t1.test_id,t1.score_date desc;

output result output 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM