gpt4 book ai didi

SQL 计数 where 子句

转载 作者:行者123 更新时间:2023-12-04 16:36:56 30 4
gpt4 key购买 nike

我有以下 SQL 语句:

 SELECT        [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId]
FROM (Leagues l INNER JOIN
Lineups lp ON l.LeagueId = lp.LeagueId)
WHERE (lp.PositionId = 1) OR
(lp.PositionId = 3) OR
(lp.PositionId = 2)

我真正需要的是获取位置计数大于数字的行。就像是:
 SELECT        [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId]
FROM (Leagues l INNER JOIN
Lineups lp ON l.LeagueId = lp.LeagueId)
WHERE Count(lp.PositionId = 1) > 2 OR
Count(lp.PositionId = 3) > 6 OR
Count(lp.PositionId = 2) > 3

有没有办法在 SQL 中做到这一点?

最佳答案

这个怎么样?:

SELECT        [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME
FROM (Leagues l INNER JOIN
Lineups lp ON l.LeagueId = lp.LeagueId)
GROUP BY [l.LeagueId], [l.LeagueName]
HAVING SUM(CASE WHEN lp.PositionId = 1 THEN 1 ELSE 0 END) > 2 OR
SUM(CASE WHEN lp.PositionId = 3 THEN 1 ELSE 0 END) > 6 OR
SUM(CASE WHEN lp.PositionId = 2 THEN 1 ELSE 0 END) > 3

关于SQL 计数 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3440857/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com