SQL查询的新手

时间:2014-09-10 09:40:11

标签: sql advantage-database-server

我是SQL新手,不明白为什么这个查询不起作用。我希望有人能帮我一把。先感谢您。

SELECT COUNT (Results.Output)
FROM RESULTS
WHERE Results.Output <> 'Simulated' AND Results.Output <> '-' AND 
      Results.Det = 'ASBACM_SZ OR Results.Det = 'ASBACM AND Result.Detdate >= ? 
      AND Result.Detdate <= ?

2 个答案:

答案 0 :(得分:2)

尝试添加一些括号并添加缺少的引号:

SELECT COUNT(Results.[Output]) FROM Results
WHERE Results.[Output] <> 'Simulated'
    AND Results.[Output] <> '-'
    AND (
        Results.Det = 'ASBACM_SZ'
            OR
        Results.Det = 'ASBACM'
    )
    AND Result.Detdate >= ?
    AND Result.Detdate <= ?

答案 1 :(得分:1)

需要关闭单引号(您错过了一些结束引号),并且当您混合使用&#39;和&#39;时,操作员通常需要进行分组。和&#39;或&#39;。

你可以试试这个:

select  count (*)
from    results
where   results.[output] <> 'Simulated'
    and results.[output] <> '-'
    and (results.det = 'ASBACM_SZ' or results.det = 'ASBACM') --group the 'or'
    and result.detdate >= 'somedate'
    and result.detdate <= 'otherdate'

缩写形式:

select  count (*)
from    results
where   results.[output] not in ('Simulated', '-')
    and results.det in ('ASBACM_SZ', 'ASBACM')
    and result.detdate between 'somedate' and 'otherdate'