简体   繁体   English

查询获取同表中customerID列的每个唯一值的表的最大主键ID

[英]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.

相关问题 SQL查询以获取另一列中每个唯一值的最大值 - SQL Query to obtain the maximum value for each unique value in another column 在表的同一列上定义的主键约束和唯一约束 - Primary key constraint and unique constraint defined on the same column of a table 如何在ID列下的同一表中插入具有不同值的相同记录? (注意:ID是主键自动递增列) - How to Insert the same records in same table with different value under Id column? (Note : ID is primary key autoincrement column ) 从SQL插入查询中获取新记录主键ID以将相同值插入另一个表中? - Get the new record primary key ID from SQL insert query to insert same value into another table? Laravel:如何查询表,从每个唯一 item_id 的列中获取第一个值并将其分配给另一个表列 - Laravel: how to query a table, take the first value of from a column for each unique item_id and assign it to another table column SQL 在同一表和查询中引用主键 - SQL refer Primary Key in same Table and Query 如何将自动生成的主键ID保存在同一表的外键列中 - How to save auto generated primary key Id in foreign key column in same table 如何使用SQL查询在同一个表上使用union all子句生成唯一的主键值? - How to generate unique primary key values with union all clause on same table with sql query? 获取表中列的最大值的最快方法是什么? - What's the fastest way to obtain the maximum value of a column in a table? 如何为表的主键创建唯一的随机整数ID? - How to to create unique random integer ID for primary key for table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM