使用Group by组获取LOD函数中的列的最大值

发布于 2025-01-22 12:58:26 字数 1473 浏览 2 评论 0原文

在产品,站点,站和日期的组合中,我希望使用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计数。

输出的屏幕截图我在下面(左侧有一个产品,单个站点和单个站点过滤掉)

我只想要每个日期具有最大dist_sn的突出显示的行

更新:这有效 https:> https: //intellipaat.com/community/7651/get-records-with-max-value-for-each-group-group-of-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-for-sql-results

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2025-01-29 12:58:26

也许您应该添加a

select 
product_code,
site_name,
station_type,
line_type,
dist_sn_count_at_blob,
date_b,
pass_blob,
fail_blob,
id 
from 
(
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, 
row_number () over (partition by  a.date_b order by a.dist_sn_count_at_blob desc) as rn
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
  )t 
where t.rn=1

Maybe you should add a row_number by on the outer query results like below and then select the highest per date.

select 
product_code,
site_name,
station_type,
line_type,
dist_sn_count_at_blob,
date_b,
pass_blob,
fail_blob,
id 
from 
(
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, 
row_number () over (partition by  a.date_b order by a.dist_sn_count_at_blob desc) as rn
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
  )t 
where t.rn=1
っ左 2025-01-29 12:58:26
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
where dist_sn_count_at_blob =
      (select max(dist_sn_count_at_blob) from insight_info_temp
        group by grouping sets  (product_code,site_name,station_type,date_b) limit 1 )
and a.product_code ='D00'
and a.site_name = 'F00'
and a.station_type='A00';
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
where dist_sn_count_at_blob =
      (select max(dist_sn_count_at_blob) from insight_info_temp
        group by grouping sets  (product_code,site_name,station_type,date_b) limit 1 )
and a.product_code ='D00'
and a.site_name = 'F00'
and a.station_type='A00';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文