Postgres-在同一个月的LOD上选择1个最大行,而不是所有最大行(组)
嗨,有一个具有产品,站点,站点,日期和一些数值字段的数据集,例如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';
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您对相同的
dist_sn_count_at_blob
有两个观察结果。这是Postgresql的
不同。
请尝试这样的事情:
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: