通过函数获得细节级别的最大列

发布于 2025-01-23 01:39:29 字数 829 浏览 0 评论 0原文

对于product_code,site_name,station_type和create_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,而不仅仅是该组中的最大值。

For a combination of product_code,site_name,station_type and created_at, I want to pick the row with the maximum dist_sn.

This is my code.

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';

This is the error I'm getting.
ERROR: column b.created_at does not exist
LINE 10: and a.created_at = b.created_at

Without converting the created_at timestamp to date, the query runs without errors. But still doesn't give the desired output. It picked up all the dist_sn instead of just the max in that group.

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

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

发布评论

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

评论(1

无声静候 2025-01-30 01:39:29

因此,解决错误所需要做的就是将别名放在内部查询中。因此,您的内部查询

 (SELECT DATE(created_at) from insight_info)) b 

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

so all you need to do to fix your error is put an alias in the inner query. So your inner query changes from

 (SELECT DATE(created_at) from insight_info)) b 

to

 (SELECT DATE(created_at) as created_at from insight_info)) b 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文