Вот моя t
таблица
+---------------+------------------------------+---------------+--+
| trading_year | sector | total_volume |
+---------------+------------------------------+---------------+--+
| 2010 | Consumer Discretionary | 119621915300 |
| 2012 | Consumer Discretionary | 93805031400 |
| 2014 | Consumer Discretionary | 80018436200 |
| 2016 | Consumer Discretionary | 81834656900 |
| 2010 | Consumer Staples | 50710518800 |
| 2012 | Consumer Staples | 39340784200 |
| 2014 | Consumer Staples | 33188087300 |
| 2016 | Consumer Staples | 37538472900 |
мне нужно пометить вышеупомянутые данные на основе максимального и минимального значения total_volume, сгруппированного по сектору
, поэтому мой вывод должен быть вот так
+---------------+------------------------------+---------------+----------------+
| trading_year | sector | total_volume | value
+---------------+------------------------------+---------------+--------------+
| 2010 | Consumer Discretionary | 119621915300 | maxvalue
| 2012 | Consumer Discretionary | 93805031400 | stable value
| 2014 | Consumer Discretionary | 80018436200 | stable value
| 2016 | Consumer Discretionary | 81834656900 | minvalue
| 2010 | Consumer Staples | 50710518800 | maxvalue
| 2012 | Consumer Staples | 39340784200 | stable value
| 2014 | Consumer Staples | 33188087300 | minvalue
| 2016 | Consumer Staples | 37538472900 | stable value
вот мой запрос, который я пробовал, но не повезло
select *,
case when total_volume = max(total_volume) then 'maxvalue'
when total_volume = min(total_volume) then 'minvalue'
else 'stable value'
end value_type
from t
group by sector,trading_year
вывод запроса выше
+---------------+------------------------------+---------------+----------------+
| trading_year | sector | total_volume | value
+---------------+------------------------------+---------------+--------------+
| 2010 | Consumer Discretionary | 119621915300 | maxvalue
| 2012 | Consumer Discretionary | 93805031400 | maxvalue
| 2014 | Consumer Discretionary | 80018436200 | maxvalue
| 2016 | Consumer Discretionary | 81834656900 | maxvalue
| 2010 | Consumer Staples | 50710518800 | maxvalue
| 2012 | Consumer Staples | 39340784200 | maxvalue
| 2014 | Consumer Staples | 33188087300 | maxvalue
| 2016 | Consumer Staples | 37538472900 | maxvalue