SQL:给定列的最多行数(从列列表中)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL:给定列的最多行数(从列列表中)相关的知识,希望对你有一定的参考价值。

我刚开始学习SQL(特别是SQLite),我一直在玩this数据。

此列表中的每个漫威角色都对每个属性(力量,智力,速度等)进行评级。我想找到每个属性最多的角色。例如,有多少个字符具有作为其最大属性的强度?

我们可以假设,如果一个角色有两个或更多属性作为最高属性,那么该角色将计入每个属性。

我期待的输出是这样的:

best_Attribute      COUNT(*)
__________________________________
intelligence            a
strength                b
speed                   c
durability              d
energy_Projection       e
fighting_Skills         f

这是我的尝试:

SELECT COUNT(*),
    CASE
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == intelligence THEN "intelligence"
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == strength THEN "strength"
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == speed THEN "speed"
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == durability THEN "durability"
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == energy_Projection THEN "energy_Projection"
        WHEN MAX(intelligence, strength, speed, durability, energy_Projection, fighting_Skills) == fighting_Skills THEN "fighting_Skills"
    END as "best_Attribute"
FROM marvels
GROUP BY best_Attribute;

这是丑陋的,冗长的,甚至不起作用。这是输出:

COUNT(*)    best_Attribute
__________________________
    2               4
    3               6
    7               7

是否有(好的)获得所需输出的方法?

答案

我不确定您是否需要确切的建议输出。我可以提供以下查询,该查询将整个表的属性计数报告为单独的列。

SELECT
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = intelligence THEN 1 END) AS intelligence_count
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = strength THEN 1 END) AS strength_count
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = speed THEN 1 END) AS speed_count
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = durability THEN 1 END) AS durability_count
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = energy_Projection THEN 1 END) AS energy_Projection_count
    COUNT(CASE WHEN MAX(intelligence, strength, speed, durability, energy_Projection,
        fighting_Skills) = fighting_Skills THEN 1 END) AS fighting_Skills_count
FROM marvels;

请注意,如果您想要整个表格的总计,则无需在此处使用GROUP BY

另一答案

蒂姆的解决方案是最高效的方法 - 以及您如何处理问题。但是,另一种方法是取消数据的使用并使用聚合。我只想提供这个作为替代方案,以便您可以在SQL中看到解决问题的不同方法:

with a as (
      select name, 'intelligence' as attribute, intelligence as val
      from marvels
      union all
      select name, 'strength' as attribute, strength as val
      from marvels
      union all
      select name, 'speed' as attribute, speed as val
      from marvels
      union all
      select name, 'durability' as attribute, durability as val
      from marvels
      union all
      select name, 'energy_Projection' as attribute, energy_Projection as val
      from marvels
      union all
      select name, 'fighting_Skills' as attribute, fighting_Skills as val
      from marvels
     )
select a.attribute, count(*) as num_with_max
from a
where a.val = (select max(a2.val) from a a2 where a2.name = a.name)
group by a.attribute;

以上是关于SQL:给定列的最多行数(从列列表中)的主要内容,如果未能解决你的问题,请参考以下文章

从列生成 MIN、AVG、MAX 列。 [SQL] 蜂巢

Python代码阅读(第25篇):将多行字符串拆分成列表

FlatMap 从列值到多行缺少架构

SQL Server将一列的多行内容拼接成一行的实现方法

pandas中的SQL查询:根据其他列的组合在列中连接多行

spark关于join后有重复列的问题(org.apache.spark.sql.AnalysisException: Reference '*' is ambiguous)(代码片段