简体   繁体   English

在SQL中,如何从表中选择*,但是当多行具有相同的字段时,只选择另一个字段B为MAX的那些?

[英]In SQL, how do I select * from a table, but when multiple rows have the same field, select only the ones where another field B is MAX?

For example, I have a Table例如,我有一个表

Id ID CarPartId汽车零件编号 CarPartPrice汽车零件价格 Metadata元数据
1 1 spanner扳手 580 580 Some other data其他一些数据
2 2 spanner扳手 570 570 Some other data 2其他数据 2
3 3 wheel车轮 423 423 Some other data其他一些数据
4 4 window窗户 234 234 Some other data其他一些数据
5 5 engine引擎 568 568 Some other data 1其他数据 1
6 6 engine引擎 423 423 Some other data 2其他数据 2

Notice that when I do a SELCT * FROM this table, I would get two rows of CarPartId, but what I really want is to get the CarPartId row whereby the CarPartPrice is the highest, along with other rows from the table.请注意,当我执行 SELCT * FROM 这个表时,我会得到两行 CarPartId,但我真正想要的是获取 CarPartId 行,其中 CarPartPrice 是最高的,以及表中的其他行。

How do I achieve this?我如何实现这一目标? For example, my query should return this例如,我的查询应该返回这个

Id ID CarPartId汽车零件编号 CarPartPrice汽车零件价格 Metadata元数据
1 1 spanner扳手 580 580 Some other data其他一些数据
3 3 wheel车轮 423 423 Some other data其他一些数据
4 4 window窗户 234 234 Some other data其他一些数据
5 5 engine引擎 568 568 Some other data 1其他数据 1

Since you say "along with other rows from the table" I understand you want to see all the rows.由于您说“连同表中的其他行”,我知道您想查看所有行。 So below will show all data but sorted with highest CarPartPrice at the top row:所以下面将显示所有数据,但在顶行以最高的 CarPartPrice 排序:

Select * From this table
Order by CarPartPrice

try this:尝试这个:

SELECT * from table INNER JOIN
  (SELECT CarPartId, MAX(CarPartPrice) as MaxPrice
    FROM table GROUP BY CarPartId
  ) grouptable
ON table.CarPartId = grouptable.CarPartId
AND table.CarPartPrice = grouptable.MaxPrice

I'd use a nested query.我会使用嵌套查询。

SELECT t1.*
  FROM Table t1
 WHERE t1.CarPartPrice = ( SELECT MAX(t2.CarPartPrice)
                             FROM Table t2
                         GROUP BY t2.CarPartId
                           HAVING t2.CarPartId = t1.CarPartId)

The nested query will give you the highest CarPartPrice per Id.嵌套查询将为您提供每个 Id 的最高 CarPartPrice。

I think what you are looking for is select max()我认为您正在寻找的是 select max()

SELECT CarPartId, MAX(CarPartPrice)
FROM this table 
GROUP BY CarPartId

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择表(A)中共享相同外键(itemId)的行,其中表中的多行具有表B中的值 - How do I select rows in table (A) sharing the same foreign key (itemId) where multiple rows in table have the values in table B 从表中选择多行,其中字段是最大日期 - Select multiple rows from a table where field is the max date 我如何 SELECT 只有表 b 中每个外键给定列的值都相同的行? - How do I SELECT only rows from table b that have all the same values for a given column per foreign key? 如何从表中选择数据,在该表中我需要返回在一个字段中具有重复值而在另一个字段中具有指定值的行? - How do I select data from a table where I need to return rows which have a repeat value in one field and a specified value in another? 如何从具有相同字段的多行中仅选择一行? - How to select only one row from multiple rows with the same field? 如何从另一个表中查找字段名的表中选择一个字段? - How do I select a field from a table where I am looking up the fieldname from another table? BigQuery 标准 SQL SELECT 行 WHERE 字段包含来自另一个表字段的单词 - BigQuery Standard SQL SELECT rows WHERE field contains words from another table field SQL SELECT仅存在最大值的行以及另一个链接表中的相应ID - SQL SELECT only rows where a max value is present, and the corresponding ID from another linked table SQL查询以选择添加日期的行在同一表的字段中的过期时间 - SQL query to select rows on date added where when they expired in a field in the same table 如何使用SQL在两个表中选择数据,其中每个表中的字段匹配? - How do i select data across two tables using SQL where a field from each table matches?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM