需求:使用sql进行分组,然后查询出各自分组内的最新一条记录
表结构:
实现:
SELECT
business_flag,
channel_code,
rate
FROM
(
SELECT
business_flag,
channel_code,
max(create_time) AS create_time_2
FROM
test
WHERE
toDate (create_time) = '2021-07-01'
GROUP BY
business_flag,
channel_code
) a
LEFT JOIN test b ON a.business_flag = b.business_flag
AND a.channel_code = b.channel_code
AND a.create_time_2 = b.create_time
思路:先对原数据进行分组去重,得到最新记录,然后依据这个结果,再获取这条记录的其他信息。
版权声明:本文为qq_38289091原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。