SQL window函数用对应的字符串替换max

SQL window function replace max by correspondent string

我有一个 table 名为 products:

+---+------------+----------------+---------------+
| Id|product_name|product_category|product_revenue|
+---+------------+----------------+---------------+
|  1|      orange|           friut|         170000|
|  2|       apple|           friut|         300000|
|  3|       peach|           friut|          98000|
|  4|      banana|           friut|         285000|
|  5|    cucumber|       vegetable|         451000|
|  6|      tomato|       vegetable|         512000|
|  7|       salad|       vegetable|         281000|
|  8|     cabbage|       vegetable|          85000|
|  9|        coke|           drink|         687000|
| 10|       fanta|           drink|         258000|
| 11|      sprite|           drink|         432000|
| 12|       pepsi|           drink|         661000|
+---+------------+----------------+---------------+

我执行此 SQL 查询:

SELECT *, max(product_revenue) OVER w AS max  
FROM products 
WINDOW w AS (PARTITION BY product_category) 
ORDER BY Id

因此得到这个 table 新的 max 列代表最大 product_revenue每个 product_category:

+---+------------+----------------+---------------+------+
| Id|product_name|product_category|product_revenue|   max|
+---+------------+----------------+---------------+------+
|  1|      orange|           friut|         170000|300000|
|  2|       apple|           friut|         300000|300000|
|  3|       peach|           friut|          98000|300000|
|  4|      banana|           friut|         285000|300000|
|  5|    cucumber|       vegetable|         451000|512000|
|  6|      tomato|       vegetable|         512000|512000|
|  7|       salad|       vegetable|         281000|512000|
|  8|     cabbage|       vegetable|          85000|512000|
|  9|        coke|           drink|         687000|687000|
| 10|       fanta|           drink|         258000|687000|
| 11|      sprite|           drink|         432000|687000|
| 12|       pepsi|           drink|         661000|687000|
+---+------------+----------------+---------------+------+

问题是我怎样才能得到通讯员product_name而不是数字? 所以我想得到这个:

+---+------------+----------------+---------------+---------+
| Id|product_name|product_category|product_revenue|   max   |
+---+------------+----------------+---------------+---------+
|  1|      orange|           friut|         170000|apple    |
|  2|       apple|           friut|         300000|apple    |
|  3|       peach|           friut|          98000|apple    |
|  4|      banana|           friut|         285000|apple    |
|  5|    cucumber|       vegetable|         451000|tomato   |
|  6|      tomato|       vegetable|         512000|tomato   |
|  7|       salad|       vegetable|         281000|tomato   |
|  8|     cabbage|       vegetable|          85000|tomato   |
|  9|        coke|           drink|         687000|coke     |
| 10|       fanta|           drink|         258000|coke     |
| 11|      sprite|           drink|         432000|coke     |
| 12|       pepsi|           drink|         661000|coke     |
+---+------------+----------------+---------------+---------+

您可以分两步完成此操作:

SELECT p.*,
       MAX(CASE WHEN maxpr = product_revenue THEN product_name END) OVER (PARTITION BY product_category)
FROM (SELECT p.*, max(product_revenue) OVER w AS maxpr
      FROM products p WINDOW w AS (PARTITION BY product_category)
     ) p
ORDER BY Id;

还有其他方法,具体取决于您的数据库。