Sql 可能的加入条件案例
我的问题是:Sql 可能的加入条件案例,sql,sql-server,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
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'
选择
当m.current0<25000时,则“有限”或“无限”结束,
m、 current0作为CurrentBalance,
m、 状态作为当前状态,
sh.DATE更改为输入PA6,
datediff(d,sh.datechanged,getdate())作为DaysPassed
来自Statush
内连接主节点m
在m.number=sh.accountid上
和sh.newstatus='pa6'
和sh.datechanged使用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())))
选择
当m.current0<25000时,则“有限”或“无限”结束
,m.current0作为当前余额
,m.status作为当前状态
,sh.date更改为输入PA6
,datediff(d,sh.datechanged,getdate())作为DaysPassed
来自Statush
m.number=sh.accountid和sh.newstatus=m.status上的内部联接主节点m
其中m.status='pa6'和m.state='ca'和
((m.current0<25000和sh.datechanged=25000和sh.datechanged)您只需使用或之类的(m.current0<25000和sh.datechanged 25000和sh.datechanged@user2989408添加此项似乎在联接中创建了一个循环。它没有向后拉197行。在我停止它之前,它向后拉了170多万行。您可以将其添加到WHERE
子句中。此外,将所有内容添加到联接子句中不是一个好的做法。只需我现在看到了一件事。m.current0正好等于25000。你没有考虑到这一点。@RossPresser想补充一点,只有当连接是内部连接时,它们才是相同的,如果是外部连接
s,它们就不一样了。
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())))