Where 子句 - 条件错误之间
Where Clause - Between Condition Error
我正在尝试处理下面的查询,我的大部分查询都是正确的,但由于某种原因,我的 BETWEEN 条件在尝试过滤掉数据集时不起作用...它根本没有过滤它。任何人都可以看看我下面的代码,让我知道我做错了什么......或者甚至可以建议一种替代方法来过滤掉从 7 月到 9 月底的记录?
SELECT A.[Name] AS ResourceName,
A.[ID] AS ResourceID,
CONVERT(VARCHAR(10),A.[STRT_Date],101) AS StartDate,
CONVERT(VARCHAR(10),A.[END_Date],101) AS EndDate,
CASE WHEN A.[END_Date] > A.[STRT_Date] THEN C2.[DY_OF_CAL_NUM] - C1.[DY_OF_CAL_NUM]
WHEN A.[END_Date] IS NULL and A.[STRT_Date] IS NOT NULL THEN C3.[DY_OF_CAL_NUM] - C1.[DY_OF_CAL_NUM]
WHEN A.[END_Date] = A.[STRT_Date] THEN 1
END AS AgeCalendar
FROM [Report] AS A
INNER JOIN [Dim] AS B
ON A.[ID] = B.[ID]
LEFT JOIN [dbo].[DT_DIM] AS C1
ON CAST(A.STRT_Date AS DATE) = C1.[CAL_DT]
LEFT JOIN [dbo].[DT_DIM] AS C2
ON CAST(A.END_Date AS DATE) = C2.[CAL_DT]
LEFT JOIN [dbo].[DT_DIM] AS C3
ON CAST(GETDATE() AS DATE) = C3.[CAL_DT]
WHERE 1 = 1
AND A.[STRT_Date] BETWEEN '07/01/2016' AND '09/30/2016'
AND A.[Name] like '%S-Info%'
OR A.[Name] like '%S-Dep%'
OR A.[Name] like '%S-Can%'
OR A.[Name] like '%S-Doc%'
用 ()
:
包装你的 Or
运算符
WHERE A.[STRT_Date] BETWEEN '07/01/2016' AND '09/30/2016'
AND (A.[Name] like '%S-Info%'
OR A.[Name] like '%S-Dep%'
OR A.[Name] like '%S-Can%'
OR A.[Name] like '%S-Doc%')
目前发生的情况是,如果一条记录有一个类似于 '%S-Doc%'
的 Name
,即使它不在日期之间也会被选中。通过包装有效 Name
s 的所有变体,您说它必须在日期和 "one of the following conditions"
之间
我正在尝试处理下面的查询,我的大部分查询都是正确的,但由于某种原因,我的 BETWEEN 条件在尝试过滤掉数据集时不起作用...它根本没有过滤它。任何人都可以看看我下面的代码,让我知道我做错了什么......或者甚至可以建议一种替代方法来过滤掉从 7 月到 9 月底的记录?
SELECT A.[Name] AS ResourceName,
A.[ID] AS ResourceID,
CONVERT(VARCHAR(10),A.[STRT_Date],101) AS StartDate,
CONVERT(VARCHAR(10),A.[END_Date],101) AS EndDate,
CASE WHEN A.[END_Date] > A.[STRT_Date] THEN C2.[DY_OF_CAL_NUM] - C1.[DY_OF_CAL_NUM]
WHEN A.[END_Date] IS NULL and A.[STRT_Date] IS NOT NULL THEN C3.[DY_OF_CAL_NUM] - C1.[DY_OF_CAL_NUM]
WHEN A.[END_Date] = A.[STRT_Date] THEN 1
END AS AgeCalendar
FROM [Report] AS A
INNER JOIN [Dim] AS B
ON A.[ID] = B.[ID]
LEFT JOIN [dbo].[DT_DIM] AS C1
ON CAST(A.STRT_Date AS DATE) = C1.[CAL_DT]
LEFT JOIN [dbo].[DT_DIM] AS C2
ON CAST(A.END_Date AS DATE) = C2.[CAL_DT]
LEFT JOIN [dbo].[DT_DIM] AS C3
ON CAST(GETDATE() AS DATE) = C3.[CAL_DT]
WHERE 1 = 1
AND A.[STRT_Date] BETWEEN '07/01/2016' AND '09/30/2016'
AND A.[Name] like '%S-Info%'
OR A.[Name] like '%S-Dep%'
OR A.[Name] like '%S-Can%'
OR A.[Name] like '%S-Doc%'
用 ()
:
Or
运算符
WHERE A.[STRT_Date] BETWEEN '07/01/2016' AND '09/30/2016'
AND (A.[Name] like '%S-Info%'
OR A.[Name] like '%S-Dep%'
OR A.[Name] like '%S-Can%'
OR A.[Name] like '%S-Doc%')
目前发生的情况是,如果一条记录有一个类似于 '%S-Doc%'
的 Name
,即使它不在日期之间也会被选中。通过包装有效 Name
s 的所有变体,您说它必须在日期和 "one of the following conditions"