我的 SQL 服务器查询没有 return 预期的结果

My SQL Server query does not return the expected result

我的 SQL 服务器查询没有 return 添加条件时的预期结果。 我在这种情况下的部分被忽略了:

chartTime >= CONVERT(datetime, '2013-06-01 00:00:00' )

它不会显示“2013-06-01 00:00:00”之后的所有记录,但在它是查询的唯一条件时有效

完成查询:

SELECT TOP(100) 
    CONVERT(smalldatetime, ROUND(CAST([chartTime] AS float) * 24.0, 0) / 24.0)[chartTime], 
    [interventionId], [terseForm], [verboseForm], 
    [attributeId], [encounterId]
FROM
    [CISReportingDB].[dbo].[PtAssessment]
WHERE
    chartTime >= CONVERT(datetime, '2013-06-01 00:00:00') 
    AND interventionId IN (SELECT [interventionId] 
                           FROM [CISReportingDB].[dbo].[PtAssessment]
                           WHERE interventionId IN (6659, 9899, 11870)) 
    OR verboseForm LIKE 'sibil. exp%' 
    OR attributeId LIKE '67194%'

谢谢

我认为你的 where 子句中需要一些括号...

WHERE
    chartTime >= CONVERT(datetime, '2013-06-01 00:00:00') 
    AND 
    (interventionId IN (SELECT [interventionId] 
                           FROM [CISReportingDB].[dbo].[PtAssessment]
                           WHERE interventionId IN (6659, 9899, 11870)) 
    OR verboseForm LIKE 'sibil. exp%' 
    OR attributeId LIKE '67194%')

这将 return 行,其中chartTime >= '2013-06-01 00:00:00'你的日期...和 ​​ 其他条件之一存在。如果没有括号,您的 AND/OR 逻辑会发生冲突,这意味着它会 return 行,其中 verboseForm LIKE 'sibil. exp%' OR attributeId LIKE '67194%'

否则,您需要确保有实际适合您过滤的行...意味着 interventionId 是 6659,9899,或 11870 或 verboseForm LIKE 'sibil. exp%' OR attributeId LIKE '67194%'.

最后,IN 子句是多余的...您可以只列出一次值:

WHERE
    chartTime >= CONVERT(datetime, '2013-06-01 00:00:00') 
    AND 
    (interventionId IN (6659, 9899, 11870) 
    OR verboseForm LIKE 'sibil. exp%' 
    OR attributeId LIKE '67194%')