Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ms sql 2012案例语法_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

ms sql 2012案例语法

ms sql 2012案例语法,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个表survey\u status\u history,其中创建了caseId、strsurveystatus和dt列 我想从表中获取所有状态的记录,但当状态为“待定”时,对于这种情况,查询应该只返回最近五天的记录 下面是我的问题 select * from survey_status_history ssh where nisactive = 1 and case when ssh.strsurveystatus = 'pending' th

我有一个表survey\u status\u history,其中创建了caseId、strsurveystatus和dt列

我想从表中获取所有状态的记录,但当状态为“待定”时,对于这种情况,查询应该只返回最近五天的记录

下面是我的问题

select *
from survey_status_history ssh
where nisactive = 1 
      and case when ssh.strsurveystatus = 'pending' 
               then ssh.dtcreated > DATEADD(DAY, 5 , GETDATE())
          end
但我的错误接近>

请建议查询中的更改。
提前感谢。

SQL Server不理解布尔类型。我建议在没有案例的情况下写下这篇文章:


这假设strsurveystatus从不为NULL。如果为空,则逻辑会稍微复杂一些。

希望,这符合您的要求

SELECT *
FROM SURVEY_STATUS_HISTORY SSH
WHERE NISACTIVE = 1 AND
(SSH.STRSURVEYSTATUS <> 'PENDING' -- if not pending
 OR(SSH.STRSURVEYSTATUS = 'PENDING' AND SSH.DTCREATED >= DATEADD(DAY, -5 , GETDATE()))) -- if pending and last 5 days

谢谢你的及时回复。如果状态为“未挂起”,则我的查询应返回所有记录,但如果状态为“挂起”,则我只需检查该记录是否为最后5天记录。@mangeshh-这似乎是Gordon提供的查询产生的结果-如果您有不同的想法,您能否提供一个Gordon的查询没有返回的行的示例,或者反之亦然?
SELECT *
FROM SURVEY_STATUS_HISTORY SSH
WHERE NISACTIVE = 1 AND
(SSH.STRSURVEYSTATUS <> 'PENDING' -- if not pending
 OR(SSH.STRSURVEYSTATUS = 'PENDING' AND SSH.DTCREATED >= DATEADD(DAY, -5 , GETDATE()))) -- if pending and last 5 days