English 中文(简体)
在Serk WHERE条款中使用化学文摘社国际协会的声明
原标题:Using a CASE statement in a SQL Server WHERE clause

页: 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
最佳回答

你们必须使用和(或)操作者,而不是使用化学文摘社。

(@StateCount > 10 AND u.StateID = @StateID) OR (@CityCount > 10 AND CityID = @CityID) OR (CountryID =  1 )

Some like this.

<>UPDADE

因此,实际上,这个例子不利于你正常工作。 在条款方面,你需要更加复杂。 引证:

(@StateCount > 10 AND u.StateID = @StateID)
OR
(@CityCount > 10 AND @StateCount <= 10 AND CityID = @CityID)
OR
(@StateCount <= 10 AND @CityCount <= 10 AND CountryID =  1 )

<><>UPDATE 2>

更糟糕的是:

(@StateCount > 10 AND u.StateID = @StateID)
OR
(@StateCount <= 10 
    AND
    (
        (@CityCount > 10 AND CityID = @CityID)
        OR
        (@CityCount <= 10 AND CountryID =  1 )
    )   
)
问题回答

暂无回答




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签