Mysql 如何在具有多个列的单行中查找具有最大值的列?
我有一张临时桌子如下:Mysql 如何在具有多个列的单行中查找具有最大值的列?,mysql,sql,greatest-n-per-group,unpivot,Mysql,Sql,Greatest N Per Group,Unpivot,我有一张临时桌子如下: |----------|------------|-------------|--------|-----------| | Country | Confirmed | Unconfirmed | Deaths | Recovered | |----------|------------|-------------|--------|-----------| | A | 95782 | 1034219 | 6723 | 2589
|----------|------------|-------------|--------|-----------|
| Country | Confirmed | Unconfirmed | Deaths | Recovered |
|----------|------------|-------------|--------|-----------|
| A | 95782 | 1034219 | 6723 | 25892 |
|----------|------------|-------------|--------|-----------|
我需要找到具有最大值的列名称(在四个给定列中)。
比如说,我需要找出A国的确诊病例、未确诊病例、死亡病例或康复病例是否更多
在这种情况下,预期输出为“未确认”,因为它在四个给定列中具有最大值 使用
多一点内容
您的完整查询如下所示
SELECT
t1.country
, ( case
WHEN t1.Confirmed = t2.maxnumber Then 'Confirmed'
WHEN t1.Unconfirmed = t2.maxnumber Then 'Unconfirmed'
WHEN t1.Deaths = t2.maxnumber Then 'Deaths'
WHEN t1.Recovered = t2.maxnumber Then 'Recovered'
ELSE 'unknown'
END) type
,t2.maxnumber
FROM table1 t1 inner join
(SELECT
Country
,GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) maxnumber
FROM table1) t2 ON t1.country = t2.country
范例
架构(MySQL v5.7)
查询#1
SELECT
t1.country
, ( case
WHEN t1.Confirmed = GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) Then 'Confirmed'
WHEN t1.Unconfirmed = GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) Then 'Unconfirmed'
WHEN t1.Deaths = GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) Then 'Deaths'
WHEN t1.Recovered = GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) Then 'Recovered'
ELSE 'unknown'
END) type
,GREATEST(Confirmed , Unconfirmed , Deaths , Recovered )
FROM table1 t1;
| country | type | GREATEST(Confirmed , Unconfirmed , Deaths , Recovered ) |
| ------- | ----------- | -------------------------------------------------------- |
| A | Unconfirmed | 1034219 |
| B | Recovered | 2225892 |
一个选项是取消PIVOT并使用
行编号()
标识每个国家/地区大多数情况下的列:
select country, case_type, nb_cases
from (
select
t.*, row_number() over(partition by country order by nb_cases desc) rn
from (
select country, 'confirmed' case_type, confirmed nb_cases from mytable
union all
select country, 'unconfirmed', unconfirmed from mytable
union all
select country, 'deaths', deaths from mytable
union all
select country, 'recovered', recovered from mytable
) t
) t
where rn = 1
:
| country | case_type | nb_cases |
| ------- | ----------- | -------- |
| A | unconfirmed | 1034219 |
您可以使用greest()
和case
:
select t.*,
(case greatest(Confirmed, Unconfirmed, Deaths, Recovered)
when Confirmed then 'Confirmed'
when Unconfirmed then 'Unconfirmed'
when Deaths then 'Deaths'
when Recovered then 'Recovered'
end) as column_greatest
from t;
两个音符
首先,如果任何列为NULL
,则这不起作用。您没有样本表明情况如此。但是如果这些值从来都不是负值,那么如果可能存在NULL
值,则可以使用COALESCE()
其次,如果存在关系,则返回第一列的最大值。再一次,您没有指定要做什么,因此这似乎是对问题的合理解释。您正在运行哪个版本的MySQL?MySQL 8.0。我认为这是最新版本,对吗?如果您使用临时表,请尝试使用转置列创建表。结构简单:
国家,键入结果,结果
。您可以使用组功能对任何结果组合进行汇总或排序。请参阅标准化。数据库表不是电子表格!!最大值实际上有效,但它只返回相应列的值,即1034219。我希望列名作为输出,它是“未确认的”。因此添加了完整的查询,我又添加了一行以显示ot worls
| country | case_type | nb_cases |
| ------- | ----------- | -------- |
| A | unconfirmed | 1034219 |
select t.*,
(case greatest(Confirmed, Unconfirmed, Deaths, Recovered)
when Confirmed then 'Confirmed'
when Unconfirmed then 'Unconfirmed'
when Deaths then 'Deaths'
when Recovered then 'Recovered'
end) as column_greatest
from t;