[英]How to get the MAX value of unique column in sql and aggregate other?
我想获得具有最大“日期”的行,仅通过唯一的“id”分组但不考虑其他列。
我试过这个查询:(但不工作导致修改其他列)
SELECT id,
MAX(num),
MAX(date),-- I just want the max of this column
MAX(product_name),
MAX(other_columns)
FROM TB
GROUP BY id
桌子:
id num date product_name other_columns
123 0001 2021-12-01 exit 12315413
123 0002 2021-12-02 entry 65481328
333 0001 2021-12-03 entry 13848136
333 ASDV 2021-12-04 exit 1325165
预期结果:
id num date product_name
123 0002 2021-12-02 entry
333 ASDV 2021-12-04 exit
怎么做?
具有内部连接的子查询可以不可知地处理这个漂亮的 DBMS。
SELECT
t.ID
,t.date
,t.product_name
,t.other_columns
FROM tb as t
INNER JOIN (
SELECT
id
,MAX(date) as date
FROM tb
GROUP BY id
) as s on t.id = s.id and t.date = s.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.