对于问题中可怕的措辞感到抱歉,正在努力解释这个问题。
我有一张这样的表:
Id Name Version
1 Chrome 38.0
2 Chrome 36.0
3 Chrome 37.0
4 Firefox 31.0
5 IE 11.0
6 IE 8.0
7 IE 7.0
我需要一个查询来返回"名称" s的值为" IE"只有"版本"是> = 8.0 否则不要返回版本,我希望结果是......
Id Name Version
1 Chrome null
2 Chrome null
3 Chrome null
4 Firefox null
5 IE 11.0
6 IE 8.0
如果这对我的存储过程有帮助,到目前为止,这将返回所有不是我想要的版本。
ALTER PROCEDURE [dbo].[GetCommonBrowserCount]
@StartDate datetime = NULL,
@EndDate datetime = NULL,
@Domain varchar(255) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT
[Name],
COUNT(Name) AS [Count],
[Version]
FROM
dbo.GetCommonBrowserAccessEvents
WHERE
(
@StartDate IS NULL OR [Date] > @StartDate
)
AND
(
@EndDate IS NULL OR [Date] < @EndDate
)
AND
(
@Domain IS NULL OR DomainName = @Domain
)
GROUP BY Name,
[Version]
END
这可能吗?
答案 0 :(得分:2)
使用case
select id,
name,
case when name = 'IE'
then version
else null
end as version
from GetCommonBrowserAccessEvents
where not
(
name = 'IE' and cast(substring(version, 1, charindex('.', version) - 1) as int) < 8
)
答案 1 :(得分:0)
限制结果集只需在AND
子句中添加另一个WHERE
:
AND
(
[Name] <> 'IE' OR [Version] > 8
)
如果NULL
退出[version]
而不是IE(为什么?),则可以使用CASE
:
SELECT
-- ...
CASE WHEN [Name] = 'IE' THEN [Version] ELSE NULL END AS Version
(如果您使用的是SQL Server 2012或更高版本,则可以使用IIF
来更简洁,但不能在2008年使用。)
答案 2 :(得分:0)
SELECT
[Name],
CASE
WHEN
[Name] = "IE" AND [Version] >= 8
THEN
[Version]
ELSE
NULL
END AS [Version]
FROM
dbo.GetCommonBrowserAccessEvents
WHERE
Name != "IE"
OR
(Name = "IE" AND Version >= 8)