我的表中有两个字段“到期日”和“解除日”,当解除日为空的时候就根据到期日来查询,当解除日不为空的时候再判断解除日和到期日哪个日期小,然后根据日期小的来查询
select  部门名称, sum(个数) 个数, sum(出保额) 出保额
from dbo.st_xiangmu_zaibao_bybm where
我的查询条件怎么写

解决方案 »

  1.   

    本帖最后由 libin_ftsafe 于 2009-02-16 10:03:52 编辑
      

  2.   

    select 部门名称, sum(个数) 个数, sum(出保额) 出保额 from dbo.st_xiangmu_zaibao_bybm where 解除日 is not null and 解除日 < 到期日 and 解除日 < 某日期? group by 部门名称 
    union all
    select 部门名称, sum(个数) 个数, sum(出保额) 出保额 from dbo.st_xiangmu_zaibao_bybm where 解除日 is not null and 到期日 < 解除日 and 到期日 < 某日期? group by 部门名称 
    union all
    select 部门名称, sum(个数) 个数, sum(出保额) 出保额 from dbo.st_xiangmu_zaibao_bybm where 解除日 is null and 到期日 < 某日期? group by 部门名称 
      

  3.   

    select  部门名称, sum(个数) 个数, sum(出保额) 出保额 
    from dbo.st_xiangmu_zaibao_bybm 
    where 当解除日=(case when 解除日 is not null and 解除日< 到期日 then 解除日 else '' end) 
    or 到期日=(case when 解除日 is null then 到期日 else '' end)
      

  4.   

    select  部门名称, sum(个数) 个数, sum(出保额) 出保额 
    from 
    (select case when 解除日 is null then 到期日 else case when 解除日>到期日 then 到期日 else 解除日 end end as dt,* from dbo.st_xiangmu_zaibao_bybm 
    ) awhere  dt ....
      

  5.   

    select 
        部门名称,sum(个数) 个数, sum(出保额) 出保额 
    from 
        dbo.st_xiangmu_zaibao_bybm 
    where 
        (case when isnull(解除日,到期日)>到期日 then 到期日 else isnull(解除日,到期日) end) ...
    isnull(解除日,到期日)>到期日 then 到期日
    当解除日不为空的时候再判断解除日和到期日哪个日期小,然后根据日期小的来查询 
    isnull(解除日,到期日)
    当解除日为空的时候就根据到期日来查询,