按功能获取详细级别组中列的最大值

Get max of a column in a level of detail group by function

对于 product_code、site_name、station_type 和 created_at 的组合,我想选择具有最大值 dist_sn 的行。

这是我的代码。

Select a.* from insight_info a,
(select insight_info.id,product_code, site_name, station_type, 
 created_at = (SELECT DATE(created_at) from insight_info),
 max(dist_sn)
 from insight_info 
 group by product_code, site_name, station_type,insight_info.id,
 created_at= (SELECT DATE(created_at) from insight_info)) b 
 where a.product_code = b.product_code
 and a.site_name = b.site_name
 and a.station_type = b.station_type
 and a.created_at = b.created_at
 and a.product_code ='D00' 
 and a.site_name = 'F00'
 and a.station_type='A00';

这是我遇到的错误。 错误:列 b.created_at 不存在 第 10 行:和 a.created_at = b.created_at

在不将 created_at 时间戳转换为日期的情况下,查询运行没有错误。但仍然没有给出所需的输出。它选择了所有 dist_sn 而不是该组中的最大值。

所以您需要做的就是在内部查询中放置一个别名来修复您的错误。所以你的内部查询从

 (SELECT DATE(created_at) from insight_info)) b 

 (SELECT DATE(created_at) as created_at from insight_info)) b