[英]get Max value without group by 1 row sql query
i need help to get a value from a table without grouping one field. 我需要帮助才能从表中获取值而不将一个字段分组。
the table design is like this 桌子设计是这样的
Progress DocNum Project
---------------------------
30 10 1111
70 11 1111
30 10 1112
100 12 1111
70 13 1112
then i want data like this. 那么我想要这样的数据。 get the max progress for each project, without grouping DocNum 获得每个项目的最大进度,而无需将DocNum分组
Progress DocNum Project
---------------------------
100 12 1111
70 13 1112
how someone can help me. 有人可以如何帮助我。 thankyou very much before 非常感谢你
You can use window functions row_number to get one row with max progress per project: 您可以使用窗口函数row_number来获得一行,每个项目的最大进度:
Select *
From (
Select t.*,
Row_number() over (partition by project order by progress desc) rn
From your_table t
) t where rn = 1
If there are multiple rows with max progress per project and you want to retrieve all of them, use rank instead: 如果有多个行,每个项目的进度最大,并且您要检索所有这些行,请使用等级代替:
Select *
From (
Select t.*,
Rank() over (partition by project order by progress desc) rnk
From your_table t
) t where rnk = 1
Both the above queries read the table only once. 以上两个查询仅读取一次该表。 The accepted solution reads the table twice. 接受的解决方案读取表两次。
You could use this 你可以用这个
SELECT t.progress, t.docnum, t.project
FROM table_name t
INNER JOIN (
SELECT MAX(progress) max_progress,
project
FROM table_name
GROUP BY project
) mt
ON t.project = mt.project
AND t.progress = mt.progress;
Or this 或这个
SELECT progress, docnum, project
FROM table_name
WHERE (progress, project) IN (
SELECT MAX(progress) max_progress,
project
FROM table_name
GROUP BY project
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.