如何从 SQL 中的子查询中获取最大值?
How to get the Max value from the subquery in SQL?
SELECT *
FROM passenger
WHERE (
SELECT COUNT(PassengerId) as total_passengers, Sex
FROM passenger
WHERE SibSp > 0
GROUP BY 2
ORDER BY 1)
我必须确定 count of male or female
在我的数据集中是否更多 atleast 1
SibSp
当我执行子查询时,我根据需要得到男性和女性计数。
如何从我的子查询中选择最大计数?
请帮忙
您不需要子查询。只需使用分组查询并添加 LIMIT 1 以获得第一行,该行具有最大计数。
SELECT Sex, COUNT(*) AS total_passengers
FROM passenger
WHERE SibSp > 0
GROUP BY Sex
ORDER BY total_passengers DESC
LIMIT 1
SELECT *
FROM passenger
WHERE (
SELECT COUNT(PassengerId) as total_passengers, Sex
FROM passenger
WHERE SibSp > 0
GROUP BY 2
ORDER BY 1)
我必须确定 count of male or female
在我的数据集中是否更多 atleast 1
SibSp
当我执行子查询时,我根据需要得到男性和女性计数。 如何从我的子查询中选择最大计数?
请帮忙
您不需要子查询。只需使用分组查询并添加 LIMIT 1 以获得第一行,该行具有最大计数。
SELECT Sex, COUNT(*) AS total_passengers
FROM passenger
WHERE SibSp > 0
GROUP BY Sex
ORDER BY total_passengers DESC
LIMIT 1