计算DIFFDATE超过指定值 - 步骤2

时间:2009-03-30 19:27:33

标签: sql

继我之前的问题。现在我有两个不同的查询。第一个查询返回5092的结果,第二个查询返回1885的结果,最多为6977.我想将它们组合成一个查询以返回6977的结果。如何使一个查询计算条件评估的数量活跃,逾期超过14天且为空(算作逾期)?提前致谢。

select count (*)
from task_conditionassessment t
    inner join taskitems_conditionassessment ti on
        t.task_conditionassessmentguid=ti.task_conditionassessmentguid  
    inner join conditionassessmentassignmentitem c on
        ti.taskitems_conditionassessmentid=c.taskitems_conditionassessmentid
where datediff(dd,c.conditionassessmentdate,t.nextduedate)>14 and t.isactive=1


select t.taskname,t.nextduedate,c.conditionassessmentdate
from task_conditionassessment t
    inner join taskitems_conditionassessment ti on
        t.task_conditionassessmentguid=ti.task_conditionassessmentguid
    inner join conditionassessmentassignmentitem c on
        ti.taskitems_conditionassessmentid=c.taskitems_conditionassessmentid
where c.conditionassessmentdate IS NULL and t.isactive=1

2 个答案:

答案 0 :(得分:1)

如果您只想要符合任何一组条件的记录数,请在or子句中使用where

select count (*)
from task_conditionassessment t
inner join taskitems_conditionassessment ti on
    t.task_conditionassessmentguid=ti.task_conditionassessmentguid  
inner join conditionassessmentassignmentitem c on
    ti.taskitems_conditionassessmentid=c.taskitems_conditionassessmentid
where (c.conditionassessmentdate IS NULL or
       datediff(dd,c.conditionassessmentdate,t.nextduedate)>14) and
      t.isactive=1

答案 1 :(得分:0)

如果我理解正确,只需OR两个条件:

select count (*)
from task_conditionassessment t
    inner join taskitems_conditionassessment ti on t.task_conditionassessmentguid=ti.task_conditionassessmentguid
    inner join conditionassessmentassignmentitem c on ti.taskitems_conditionassessmentid=c.taskitems_conditionassessmentid
where (datediff(dd,c.conditionassessmentdate,t.nextduedate)>14
        or c.conditionassessmentdate IS NULL)
    and t.isactive=1