我有以下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