我正在尝试WHERE
根据每个州或城市中存在的人数来更改条款。
如果一个州有超过 10 人,我希望WHERE
是:
WHERE u.StateID = @StateID
如果一个城市有超过 10 个人,我希望它是:
WHERE u.CityID = @CityID
代替
WHERE u.StateID = @StateID
如果城市或州的人数不超过 10 人,我希望它是:
WHERE u.CountryID = '1'
ALTER PROCEDURE GetHighscore
(@UserID int)
AS
BEGIN
Declare @StateCount int
Declare @CityCount int
Declare @StateID int
Declare @CityID int
SELECT @StateID=StateID FROM tblUser WHERE UserID = @UserID
SELECT @CityID=CityID FROM tblUser WHERE UserID = @UserID
SELECT @StateCount=COUNT(DISTINCT tblUserTrix.UserID)
FROM tblUserTrix INNER JOIN
tblUser ON tblUserTrix.UserID = tblUser.UserID
WHERE (tblUser.StateID = @StateID)
SELECT @CityCount=COUNT(DISTINCT tblUserTrix.UserID)
FROM tblUserTrix INNER JOIN
tblUser ON tblUserTrix.UserID = tblUser.UserID
WHERE (tblUser.CityID = @CityID)
SELECT TOP 10 ut.UserID, SUM(t.Hardness) AS TotalTrixPoints, u.FirstName, u.LastName, u.StateID, u.CityID, tblSweCitys.CityName
FROM tblUserTrix AS ut INNER JOIN
tblUser AS u ON ut.UserID = u.UserID INNER JOIN
tblState ON u.StateID = tblState.StateID INNER JOIN
tblCitys ON u.CityID = tblCitys.CityID LEFT OUTER JOIN
tblTrix AS t ON ut.TrixID = t.TrixID
WHERE CASE
WHEN @StateCount > 10
THEN u.StateID = @StateID
WHEN @CityCount > 10
THEN u.CityID = @CityID
ELSE u.CountryID = '1'
END = ?
GROUP BY ut.UserID, u.FirstName, u.LastName, u.CityID, u.StateID, tblCitys.CityName
ORDER BY TotalTrixPoints DESC
END