Postgres-在同一个月的LOD上选择1个最大行,而不是所有最大行(组)

发布于 2025-01-22 19:24:05 字数 1048 浏览 0 评论 0原文

嗨,有一个具有产品,站点,站点,日期和一些数值字段的数据集,例如SN_Count,Blob等。 在产品,站点和站的每一个组合中,如果同个月有不同日期的多个条目,我只想在该月使用Max SN计数一行。

我现在拥有的代码大部分时间正在工作。在那个月,它以较小的SN计数过滤排出。但这给我所有的行都具有相同的最大sn计数。而我只想一个月的1个。

这是我的代码:

FROM insight_info_temp a
INNER JOIN
(
    SELECT distinct b.product_code,b.site_name,b.station_type,to_char(b.date_b, 'YYYY-MM') as date_new, 
    MAX(dist_sn_count_at_blob) as max_sn
    FROM insight_info_temp b
    GROUP BY b.product_code,b.site_name,b.station_type,to_char(b.date_b, 'YYYY-MM')
) b
    ON a.product_code = b.product_code and
    a.site_name = b.site_name and
    a.station_type = b.station_type and
    to_char(a.date_b, 'YYYY-MM') = b.date_new
    AND a.dist_sn_count_at_blob = b.max_sn
    where a.product_code = 'D00' 
    and a.site_name = 'F00' and a.station_type = 'A00';
    

这是我的结果:

突出显示的行具有相同的SN计数,是该月的最大SN计数。 但是,我只想要这些行之一。不是两个。

Hi have a data set which has product, site, station, date and some numerical fields such as sn_count, blob etc.
Within every combination of product, site and station, if there are multiple entries for the same month from different dates, I want to only pick one row with max sn count in that month.

The code I have right now is working for the most part. It is filtering out rows with lesser sn counts in that month. But it gives me all rows with the same max sn count. Whereas, I just want 1 from a month.

This is my code:

FROM insight_info_temp a
INNER JOIN
(
    SELECT distinct b.product_code,b.site_name,b.station_type,to_char(b.date_b, 'YYYY-MM') as date_new, 
    MAX(dist_sn_count_at_blob) as max_sn
    FROM insight_info_temp b
    GROUP BY b.product_code,b.site_name,b.station_type,to_char(b.date_b, 'YYYY-MM')
) b
    ON a.product_code = b.product_code and
    a.site_name = b.site_name and
    a.station_type = b.station_type and
    to_char(a.date_b, 'YYYY-MM') = b.date_new
    AND a.dist_sn_count_at_blob = b.max_sn
    where a.product_code = 'D00' 
    and a.site_name = 'F00' and a.station_type = 'A00';
    

This is the result I have:
enter image description here

The highlighted rows have the same sn count and is the max sn count for that month.
I however, only want one of these rows. Not both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

临走之时 2025-01-29 19:24:05

我的猜测是,您对相同的dist_sn_count_at_blob有两个观察结果。

这是Postgresql的 不同。

请尝试这样的事情:

select distinct on (product_code, site_name, station_type, 
                    to_char(date_b, 'YYYY-MM'))
       dist_sn_count_at_blob, last_updated_at_pkey, <other columns>
  from insight_info_temp 
 where a.product_code = 'D00' 
   and a.site_name = 'F00' 
   and a.station_type = 'A00'
 order by product_code, site_name, station_type, 
          to_char(date_b, 'YYYY-MM'), dist_sn_count_at_blob desc;

My guess is that you have two observations with the same dist_sn_count_at_blob.

This is a candidate for PostgreSQL's distinct on.

Please try something like this:

select distinct on (product_code, site_name, station_type, 
                    to_char(date_b, 'YYYY-MM'))
       dist_sn_count_at_blob, last_updated_at_pkey, <other columns>
  from insight_info_temp 
 where a.product_code = 'D00' 
   and a.site_name = 'F00' 
   and a.station_type = 'A00'
 order by product_code, site_name, station_type, 
          to_char(date_b, 'YYYY-MM'), dist_sn_count_at_blob desc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文