sql group by和max以及其他值

编程入门 行业动态 更新时间:2024-10-26 16:35:09
本文介绍了sql group by和max以及其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一张包含以下内容的表:

i have a table that contains:

itemid inventdimid datephysical transrefid 10001 123 2015-01-02 300002 10002 123 2015-01-03 3566 10001 123 2015-02-05 55555 10002 124 2015-02-01 4545

我想要的结果

itemid inventdimid datephysical transrefid 10001 123 2015-02-05 555 10002 123 2015-01-03 3566 10002 124 2015-02-01 4545

MY查询:

MY query:

SELECT a.itemid,a.inventdimid,max(a.datephysical),a.transrefid FROM a where dataareaid = 'ermi' group by a.itemid,a.inventdimid

它在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

it is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

推荐答案

使用ANSI标准 row_number() 功能:

Use the ANSI standard row_number() function:

select t.* from (select t.*, row_number() over (partition by itemid, inventdimid order by datephysical desc) as seqnum from table t ) t where seqnum = 1;

更多推荐

sql group by和max以及其他值

本文发布于:2023-10-20 01:09:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:以及其他   sql   group   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!