关于加入标准的案例

时间:2013-12-20 17:29:26

标签: sql sql-server

这是我的问题:

select
    case when m.current0 < 25000 then 'Limited' else 'Unlimited' end,
    m.current0 as CurrentBalance,
    m.status as CurrentStatus,
    sh.datechanged as EntertedPA6,
    datediff(d, sh.datechanged, getdate()) as DaysPassed 
from StatusHistory sh
inner join master m 
  on m.number = sh.accountid 
  and sh.newstatus = 'pa6' 
  and sh.datechanged <= DATEADD(day, -20, GETDATE()) 
  and m.status = 'pa6' 
  and m.state = 'ca'

与select的第一行中的case语句类似。我需要在连接标准上有类似的东西。

If m.current0 < 25000 then sh.datechanged <= DATEADD(day, -20, GETDATE())
If m.current0 > 25000 then sh.datechanged <= DATEADD(day, -90, GETDATE())

这可能吗?还是我需要做一个工会?

1 个答案:

答案 0 :(得分:2)

使用OR和AND可以执行类似

的操作
select
    case when m.current0 < 25000 then 'Limited' else 'Unlimited' end
    ,m.current0 as CurrentBalance
    ,m.status as CurrentStatus
    ,sh.datechanged as EntertedPA6
    ,datediff(d, sh.datechanged, getdate()) as DaysPassed 
from StatusHistory sh
    inner join master m on m.number = sh.accountid and sh.newstatus = m.status           
WHERE m.status = 'pa6' AND m.state = 'ca' AND
    ((m.current0 < 25000 AND sh.datechanged <= DATEADD(day, -20, GETDATE()))
     OR (m.current0 >= 25000 AND sh.datechanged <= DATEADD(day, -90, GETDATE())))