使用Group by组获取LOD函数中的列的最大值
在产品,站点,站和日期的组合中,我希望使用Max Dist_sn_count_at_blob的行。
代码:
Select a.product_code,a.site_name,a.station_type,a.line_type,a.dist_sn_count_at_blob,a.date_b,
a.pass_blob,a.fail_blob,
a.id from insight_info_temp a,
(select insight_info_temp.id,product_code, site_name, station_type,
date_b,
max(dist_sn_count_at_blob)
from insight_info_temp
group by product_code, site_name, station_type,insight_info_temp.id,
date_b) b
where a.product_code = b.product_code
and a.site_name = b.site_name
and a.station_type = b.station_type
and a.date_b = b.date_b
and a.product_code ='D00'
and a.site_name = 'F00'
and a.station_type='A00'
and a.id = b.id;
问题:我根据需要获得每个日期一行。我将获得所有日期的所有行,包括所有SN计数。
输出的屏幕截图我在下面(左侧有一个产品,单个站点和单个站点过滤掉)
On a combination of product, site, station and date I want the row with the max dist_sn_count_at_blob.
Code:
Select a.product_code,a.site_name,a.station_type,a.line_type,a.dist_sn_count_at_blob,a.date_b,
a.pass_blob,a.fail_blob,
a.id from insight_info_temp a,
(select insight_info_temp.id,product_code, site_name, station_type,
date_b,
max(dist_sn_count_at_blob)
from insight_info_temp
group by product_code, site_name, station_type,insight_info_temp.id,
date_b) b
where a.product_code = b.product_code
and a.site_name = b.site_name
and a.station_type = b.station_type
and a.date_b = b.date_b
and a.product_code ='D00'
and a.site_name = 'F00'
and a.station_type='A00'
and a.id = b.id;
Problem: I am not getting one row per date as desired. I am getting all the rows for all dates, including all the sn counts.
Screenshot of output I got below(on the left there's a single product, single site, and single station filtered out)
I only wanted the highlighted rows with the max dist_sn per date
Update: This worked
https://intellipaat.com/community/7651/get-records-with-max-value-for-each-group-of-grouped-sql-results
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该添加a
Maybe you should add a row_number by on the outer query results like below and then select the highest per date.