从字段名称为MySql

时间:2019-02-05 15:10:41

标签: mysql sql

这是我的桌子的样子:

ID    USER    VALUE1   VALUE2   VALUE3   VALUE4    VALUE5
2TG   Admin     20       45       10       12        24
5HT   Supp      35       10       35       18        31
8PK   Exc       80       62       01       38        70
1JH   MANG      48       96       21       23        36
4YT   LAW       58       50       29       58        51     

我正在使用的代码:

SELECT ID, USER,  MAX(MAX1) AS MAX, ValueName FROM ( SELECT ID, USER,Value1 AS MAX1, 'Value1' as ValueName FROM Data

UNION
SELECT ID,USER, Value2 AS MAX1, 'Value2' as ValueName
FROM Data 
UNION
SELECT ID, USER,Value3 AS MAX1, 'Value3' as ValueName
FROM Data
UNION
SELECT ID, USER,Value4 AS MAX1, 'Value4' as ValueName
FROM Data
UNION
SELECT ID, USER,Value5 AS MAX1, 'Value5' as ValueName
FROM Data) T  GROUP BY ID

使用上述代码,这就是我得到的输出:

ID    USER    MAX1   ValueName
2TG   Admin    45    Value1
5TH   Supp     35    Value1
8PK   Exc      80    Value1
1JH   MANG     96    Value1
4YT   LAW      58    Value1    

这将是所需的输出:

 ID    USER    MAX1   MAX2   MAX3   ValueName1  ValueName2   ValueName3
 2TG   Admin    45     24     20    Value2      Value5       Value1 
 5TH   Supp     35     35     31    Value1      Value3       Value5
 8PK   Exc      80     70     62    Value1      Value5       Value2
 1JH   MANG     96     48     36    Value2      Value1       Value5
 4YT   LAW      58     58     51    Value1      Value4       Value5

我想做的是获得行中的最大值1(最高),最大值2(第二最高)和最大值3(第三最高)以及列1,2所在列的名称,找到或属于3个最大值。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在MySQL 8.x中,您可以执行以下操作:

with x as (
  select id, Value1 as val, 'Value1' as col from data
  union all select id, Value2, 'Value2' from data
  union all select id, Value3, 'Value3' from data
  union all select id, Value4, 'Value4' from data
  union all select id, Value5, 'Value5' from data
),
y as (
  select id, val, col,
    row_number() over (partition by id order by val desc) as rn
  from x
)
select
  d.id, d.user,
  v1.val as max1, v1.col as ValueName1,
  v2.val as max2, v2.col as ValueName2,
  v3.val as max3, v3.col as ValueName3
from data d
join (
  select y.id, y.val, y.col
  from y join data d on d.id = y.id
  where rn = 1
) v1 on v1.id = d.id
join (
  select y.id, y.val, y.col
  from y join data d on d.id = y.id
  where rn = 2
) v2 on v2.id = d.id
join (
  select y.id, y.val, y.col
  from y join data d on d.id = y.id
  where rn = 3
) v2 on v2.id = d.id

答案 1 :(得分:1)

如果您接受略有不同的结果格式,则可以使用group_concat()

SELECT ID, USER,
       SUBSTRING_INDEX(GROUP_CONCAT(max1 ORDER BY max1 DESC) , ',', 3) as Top3_names,
       SUBSTRING_INDEX(GROUP_CONCAT(ValueName ORDER BY max1 DESC) , ',', 3) as Top3_names,
FROM (SELECT ID, USER, Value1 AS MAX1, 'Value1' as ValueName FROM Data
      UNION ALL
      SELECT ID,USER, Value2 AS MAX1, 'Value2' as ValueName
      FROM Data 
      UNION
      SELECT ID, USER,Value3 AS MAX1, 'Value3' as ValueName
      FROM Data
      UNION
      SELECT ID, USER,Value4 AS MAX1, 'Value4' as ValueName
      FROM Data
      UNION
      SELECT ID, USER,Value5 AS MAX1, 'Value5' as ValueName
      FROM Data
     ) T
GROUP BY ID;

实际上,您可以解析出所需的数据:

SELECT ID, USER,
       SUBSTRING_INDEX(GROUP_CONCAT(max1 ORDER BY max1 DESC), ',', 1) as max1,
       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(max1 ORDER BY max1 DESC), ',', 2), ',', -1) as max2,
       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(max1 ORDER BY max1 DESC), ',', 3), ',', -1) as max3,
       SUBSTRING_INDEX(GROUP_CONCAT(valuename ORDER BY max1 DESC), ',', 1) as valuename1,
       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(valuename, ORDER BY max1 DESC), ',', 2), ',', -1) as valuename2,
       SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(valuename, ORDER BY max1 DESC), ',', 3), ',', -1) as valuename3
FROM . . .

还有另一种使用变量的方法,但是如果您没有窗口函数,则该方法可能是最简单的方法。