Having Statement 和 Where Statement 产生关于口袋妖怪数据的不一致结果
Having Statement and Where Statement producing inconsistent results regarding Pokemon data
这是我的第一个问题。请放轻松!
我正在做一个 SQL project in KhanAcademy. My source data are lines 1-181 of this pokemon database(即所有第 1 代口袋妖怪),我正在 运行 查询有关口袋妖怪的攻击统计数据。
运行 SELECT Type_1, COUNT(*) FROM pokemon WHERE "Attack" > 120 GROUP BY Type_1;
,我得到了 15 个按 9 种不同类型分组的神奇宝贝,这正是我所期望的。
但是,当我尝试使用 HAVING 语句查找至少有一只宠物小精灵的攻击力超过 120 的宠物小精灵的种类数量时,它 returns 只有 4 种。
这是代码:SELECT Type_1 AS "type" FROM pokemon GROUP BY type_1 HAVING "Attack" > 120;
。
举个例子,我的第二个查询中缺少一只神奇宝贝是 Machamp:INSERT INTO pokemon(Number,Name,Type_1,Type_2,Total,HP,Attack,Defense,Sp_Atk,Sp_Def,Speed,Generation,Legendary) VALUES (68,'Machamp','Fighting',NULL,505,90,130,80,65,85,55,1,'False');
我不确定我的第二个查询有什么问题。如果可以的话请帮忙。
这个查询:
SELECT Type_1 AS "type"
FROM pokemon
GROUP BY type_1
HAVING "Attack" > 120;
无效 SQL 并且应该返回错误。为什么?因为 HAVING
发生在 之后 GROUP BY
。并且 Attack
未定义。它需要在激活函数中,例如:
HAVING MAX(Attack) > 120
此版本的查询在逻辑上等同于:
SELECT p.type
FROM (SELECT Type_1 AS "type", MAX(Attack) as max_attack
FROM pokemon
GROUP BY type_1
) p
WHERE max_Attack > 120
I attempt a HAVING
statement to find the number of types of Pokemon with at least one pokemon with over 120 Attack
如果你想计算有多少种宠物小精灵至少有一只宠物小精灵的攻击次数超过 120 次,那么你可以在现有查询之上添加另一级别的聚合,例如:
select count(*) no_types
from (select 1 from pokemon where attack > 120 group by type_1) t
请注意,子查询使用 select 1
:我们实际上不需要它 return 某些特定列,我们只想知道它包含多少行 - 因此 select 1
, 这使得意图明确。
或者,我们可以使用 count(distinct)
,这样就不需要子查询了:
select count(distinct type_1) no_types from pokemon where attack > 120
这是我的第一个问题。请放轻松!
我正在做一个 SQL project in KhanAcademy. My source data are lines 1-181 of this pokemon database(即所有第 1 代口袋妖怪),我正在 运行 查询有关口袋妖怪的攻击统计数据。
运行 SELECT Type_1, COUNT(*) FROM pokemon WHERE "Attack" > 120 GROUP BY Type_1;
,我得到了 15 个按 9 种不同类型分组的神奇宝贝,这正是我所期望的。
但是,当我尝试使用 HAVING 语句查找至少有一只宠物小精灵的攻击力超过 120 的宠物小精灵的种类数量时,它 returns 只有 4 种。
这是代码:SELECT Type_1 AS "type" FROM pokemon GROUP BY type_1 HAVING "Attack" > 120;
。
举个例子,我的第二个查询中缺少一只神奇宝贝是 Machamp:INSERT INTO pokemon(Number,Name,Type_1,Type_2,Total,HP,Attack,Defense,Sp_Atk,Sp_Def,Speed,Generation,Legendary) VALUES (68,'Machamp','Fighting',NULL,505,90,130,80,65,85,55,1,'False');
我不确定我的第二个查询有什么问题。如果可以的话请帮忙。
这个查询:
SELECT Type_1 AS "type"
FROM pokemon
GROUP BY type_1
HAVING "Attack" > 120;
无效 SQL 并且应该返回错误。为什么?因为 HAVING
发生在 之后 GROUP BY
。并且 Attack
未定义。它需要在激活函数中,例如:
HAVING MAX(Attack) > 120
此版本的查询在逻辑上等同于:
SELECT p.type
FROM (SELECT Type_1 AS "type", MAX(Attack) as max_attack
FROM pokemon
GROUP BY type_1
) p
WHERE max_Attack > 120
I attempt a
HAVING
statement to find the number of types of Pokemon with at least one pokemon with over 120 Attack
如果你想计算有多少种宠物小精灵至少有一只宠物小精灵的攻击次数超过 120 次,那么你可以在现有查询之上添加另一级别的聚合,例如:
select count(*) no_types
from (select 1 from pokemon where attack > 120 group by type_1) t
请注意,子查询使用 select 1
:我们实际上不需要它 return 某些特定列,我们只想知道它包含多少行 - 因此 select 1
, 这使得意图明确。
或者,我们可以使用 count(distinct)
,这样就不需要子查询了:
select count(distinct type_1) no_types from pokemon where attack > 120