[英]Query to obtain the maximum primary key ID of a table for each unique value of the customerID column in the same table
I'm trying to run a query that takes obtains the maximum QuestionnaireId value for each unique value in column VendorId.我正在尝试运行一个查询,该查询获取 VendorId 列中每个唯一值的最大 QuestionnaireId 值。
So for example from this table:因此,例如从这张表中:
QuestionnaireId VendorId
1 10003
2 10004
3 10004
4 10006
5 10005
6 10007
7 10005
8 10005
I would obtain:我会得到:
QuestionnaireId VendorId
1 10003
3 10004
8 10005
4 10006
6 10007
I'm using the following code to get the maximum QuestionnaireId, but need another statement alongside it to get the unique VendorIds as well.我正在使用以下代码来获取最大的 QuestionnaireId,但还需要在它旁边添加另一个语句来获取唯一的 VendorIds。 Note that the statement I've included is just last segment of a large Join function to combine all of my tables into one.
请注意,我包含的语句只是将所有表合并为一个的大型 Join 函数的最后一段。
WHERE Questionnaire.QuestionnaireId = (SELECT MAX(Questionnaire.QuestionnaireId) FROM Questionnaire)
Just use aggregation:只需使用聚合:
select max(q.QuestionnaireId) as QuestionnaireId, VendorId
from Questionnaire
group by VendorId;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.