简体   繁体   English

如何在使用 max 和 group by 时 select 不同的对应列

[英]How to select different corresponding column while using max and group by

maybe simple question but I have this table:也许很简单的问题,但我有这张桌子:

+----+----------+-------------+------------------+---------------------+---------+
| id | realtyId | priceTypeId | price            | date                | comment |
+----+----------+-------------+------------------+---------------------+---------+
|  1 |        1 |           1 |     7.1100000000 | 2022-07-16 20:51:47 | []      |
|  2 |        2 |           1 |     2.3400000000 | 2022-07-16 21:01:05 | []      |
|  3 |        2 |           2 | 23950.0000000000 | 2022-07-16 21:03:58 | []      |
|  4 |        4 |           1 |     6.1800000000 | 2022-07-16 21:27:59 | []      |
|  5 |        5 |           1 |     6.1800000000 | 2022-07-16 21:28:12 | []      |
|  6 |        6 |           1 |     6.1800000000 | 2022-07-16 21:28:23 | []      |
|  7 |        7 |           1 |     3.9200000000 | 2022-07-16 21:28:37 | []      |
|  8 |       10 |           1 |     3.4500000000 | 2022-07-16 22:01:05 | []      |
|  9 |       11 |           1 |     4.6600000000 | 2022-07-16 22:15:37 | []      |
| 10 |       16 |           1 |     4.2400000000 | 2022-07-16 22:23:25 | []      |
| 11 |       10 |           4 | 45000.0000000000 | 2022-07-16 22:28:22 | []      |
| 12 |       16 |           4 | 45000.0000000000 | 2022-07-16 22:35:40 | []      |
| 13 |        6 |           4 | 25000.0000000000 | 2022-07-16 22:37:27 | []      |
| 14 |       16 |           4 |  4633.0000000000 | 2022-07-31 16:56:33 | []      |
| 15 |        7 |           4 | 25584.0000000000 | 2022-07-31 16:57:11 | []      |
| 16 |        4 |           4 |  8485.0000000000 | 2022-07-31 18:32:36 | []      |
+----+----------+-------------+------------------+---------------------+---------+

I need to get the price of the highest priceTypeId for a realtyId.我需要获取 realtyId 的最高 priceTypeId 的价格。 While I try this:当我尝试这个时:

select id,realtyId,max(priceTypeId),price from prices group by realtyId

I get我明白了

+----+----------+------------------+--------------+
| id | realtyId | max(priceTypeId) | price        |
+----+----------+------------------+--------------+
|  1 |        1 |                1 | 7.1100000000 |
|  2 |        2 |                2 | 2.3400000000 |
|  4 |        4 |                4 | 6.1800000000 |
|  5 |        5 |                1 | 6.1800000000 |
|  6 |        6 |                4 | 6.1800000000 |
|  7 |        7 |                4 | 3.9200000000 |
|  8 |       10 |                4 | 3.4500000000 |
|  9 |       11 |                1 | 4.6600000000 |
| 10 |       16 |                4 | 4.2400000000 |
+----+----------+------------------+--------------+

But I need to get the corresponding price for the max of priceTypeId (for example id 2 should be 23950)但是我需要获取 priceTypeId 最大值对应的价格(例如 id 2 应该是 23950)

Also.还。 if there are two or more priceTypeIds with the same number for the same realtyId, it should take the latest one (thats the purpose of date column)如果同一个 realtyId 有两个或多个 priceTypeIds 具有相同的数字,它应该采用最新的一个(这就是日期列的目的)

Thank you if U can help me如果你能帮助我,谢谢

You either aggregate a column in your SELECT clause with an aggregation formula or you include that column in your GROUP BY clause.您可以使用聚合公式聚合SELECT子句中的列,或者将该列包含在GROUP BY子句中。 By adding price to your SELECT clause and not aggregating it via formula or putting it in your GROUP BY clause, you are telling mysql "If there are multiple prices for this GROUP BY, I don't care which one you pick, surprise me" .通过将price添加到您的SELECT子句而不是通过公式汇总或将其放入您的GROUP BY子句中,您是在告诉 mysql “如果此 GROUP BY 有多个价格,我不在乎您选择哪一个,让我感到惊讶” . Newer versions of mysql error out with SQL like this, as does every other RDBMS. mysql 的较新版本与 SQL 类似,与其他所有 RDBMS 一样。 This "feature" of mysql has led many people to misunderstand what GROUP BY is doing, what its limitations are, and how to use it properly. mysql的这个“特性”导致很多人误解了GROUP BY是做什么的,它的局限性是什么,以及如何正确使用它。

At any rate, a correlated subquery will solve your requirements:无论如何,相关子查询将解决您的要求:

SELECT id,realtyId, priceTypeID ,price 
FROM prices p1
WHERE priceTypeID = 
   (
       SELECT max(priceTypeID) 
       FROM prices p2
       WHERE p1.id = p2.id
          AND p1.realtyID = p2.realtyID
   )

First thing you'll need is the highest priceTypeId by realtyId.您需要的第一件事是 realtyId 的最高 priceTypeId。

SELECT realtyId, max(priceTypeId) AS priceTypeId
  FROM prices
 GROUP BY realtyId;

With that you could select all matching prices:有了它,您可以 select 所有匹配的价格:

WITH maxTypeIds AS (
  SELECT realtyId, max(priceTypeId) AS priceTypeId
    FROM prices
   GROUP BY realtyId
)
SELECT * FROM prices 
 INNER JOIN maxTypeIds USING (realtyId, priceTypeId);

Now, it could happen, that there are multiple prices (different ids) having the same priceTypeId, in that case, you want to have the most recent one.现在,可能会发生具有相同 priceTypeId 的多个价格(不同的 id),在这种情况下,您希望拥有最新的价格。 One can do the same thing again, but with max(date) instead of max(priceTypeId) :人们可以再次做同样的事情,但使用max(date)而不是max(priceTypeId)

WITH maxTypeIds AS (
  SELECT realtyId, max(priceTypeId) AS priceTypeId
    FROM prices
   GROUP BY realtyId
), maxPrices AS (
  SELECT * FROM prices 
   INNER JOIN maxTypeIds USING (realtyId, priceTypeId)
), lastPrices AS (
  SELECT realtyId, max(date) AS date 
    FROM maxPrices
   GROUP BY realtyId
)    
SELECT maxPrices.* FROM maxPrices
 INNER JOIN lastPrices USING (realtyId, date);

results in结果是

realtyId不动产ID priceTypeId priceTypeId id ID price价格 date日期 comment评论
1 1 1 1 1 1 7.1100000000 7.1100000000 2022-07-16 20:51:47 2022-07-16 20:51:47 [] []
2 2 2 2 3 3 23950.0000000000 23950.0000000000 2022-07-16 21:03:58 2022-07-16 21:03:58 [] []
5 5 1 1 5 5 6.1800000000 6.1800000000 2022-07-16 21:28:12 2022-07-16 21:28:12 [] []
11 11 1 1 9 9 4.6600000000 4.6600000000 2022-07-16 22:15:37 2022-07-16 22:15:37 [] []
10 10 4 4 11 11 45000.0000000000 45000.0000000000 2022-07-16 22:28:22 2022-07-16 22:28:22 [] []
6 6 4 4 13 13 25000.0000000000 25000.0000000000 2022-07-16 22:37:27 2022-07-16 22:37:27 [] []
16 16 4 4 14 14 4633.0000000000 4633.0000000000 2022-07-31 16:56:33 2022-07-31 16:56:33 [] []
7 7 4 4 15 15 25584.0000000000 25584.0000000000 2022-07-31 16:57:11 2022-07-31 16:57:11 [] []
4 4 4 4 16 16 8485.0000000000 8485.0000000000 2022-07-31 18:32:36 2022-07-31 18:32:36 [] []

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

相关问题 MySQL在使用GROUP BY WEEK时通过SQL中的另一列选择max(column)的行? - MySQL select rows by max(column) by another column in SQL while using GROUP BY WEEK? 如何为DATE分组的MAX DATE在mysql中选择相应的列 - How to select a corresponding column in mysql for a MAX DATE grouped by DATE 如何从不同的列中获取对应的最大单元格值 - How to get corresponding cell value of max from different column 在MySQL中对不同值进行GROUP BY时,如何基于MAX值选择列 - How to select column based on the MAX value when doing GROUP BY on different value in MySQL 使用MAX和GROUP BY选择所有相应的字段 - Selecting all corresponding fields using MAX and GROUP BY 如何 select 一列具有其他列的最大值,以及另一个表中列的所有相应行? - How to select a column with max value of other column, and all corresponding rows of column in another table? 如何在SQL的相应列中选择不同值的计数? - How to select count of different values in the corresponding column in SQL? 在使用另一个GROUP BY时选择具有MAX值的行 - Select Row with MAX value while using another GROUP BY MySQL:使用不同表中的值从组中选择最大值? - MySQL: Select max from group using the value from a different table? 选择在MySQL Group中获取与max对应的整行 - Select get entire row corresponding to max in MySQL Group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM