页: 1 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