Sql f函数仅在一个位置。看看你是如何为每个案例陈述复制它的?这使得维护起来很麻烦。@JamesWilson至于colorThresholds cte,可以很容易地用您的CASE语句替换。当您想要将查询分解为更小的部分时,可以使用CTE。我喜欢在其他地方(如wh

Sql f函数仅在一个位置。看看你是如何为每个案例陈述复制它的?这使得维护起来很麻烦。@JamesWilson至于colorThresholds cte,可以很容易地用您的CASE语句替换。当您想要将查询分解为更小的部分时,可以使用CTE。我喜欢在其他地方(如wh,sql,tsql,Sql,Tsql,f函数仅在一个位置。看看你是如何为每个案例陈述复制它的?这使得维护起来很麻烦。@JamesWilson至于colorThresholds cte,可以很容易地用您的CASE语句替换。当您想要将查询分解为更小的部分时,可以使用CTE。我喜欢在其他地方(如where子句、联接或其他列的一部分)需要计算/计算列时使用它们。非常努力地避免重复代码。谢谢你的回答,它工作得很好,运行速度也很快。谢谢你花这么多时间解释,我很感激。 select m.number, 'status1', 'Green', da


f函数仅在一个位置。看看你是如何为每个案例陈述复制它的?这使得维护起来很麻烦。@JamesWilson至于colorThresholds cte,可以很容易地用您的CASE语句替换。当您想要将查询分解为更小的部分时,可以使用CTE。我喜欢在其他地方(如where子句、联接或其他列的一部分)需要计算/计算列时使用它们。非常努力地避免重复代码。谢谢你的回答,它工作得很好,运行速度也很快。谢谢你花这么多时间解释,我很感激。
select m.number, 'status1', 'Green', datediff(day, (select max(datechanged) from statushistory where accountid = m.number), getdate())
from master m with (nolock)
inner join customer c with (nolock) on m.customer = c.customer
where m.status = 'status1'
and datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) <= 1
and qlevel < 998
and (m.desk not like 'ATY%')
and (isnull(m.link,0) = 0 or m.linkdriver = 1)
and m.desk not in ('param1','param2','param3','param4','param5','param6','param7','param8','param9','param10','param11','param12')

select m.number, 'status1', 'Yellow', datediff(day, (select max(datechanged) from statushistory where accountid = m.number), getdate())
from master m with (nolock)
inner join customer c with (nolock) on m.customer = c.customer
where m.status = 'status1'
and datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) between 1 and 2
and qlevel < 998
and (m.desk not like 'ATY%')
and (isnull(m.link,0) = 0 or m.linkdriver = 1)
and m.desk not in ('param1','param2','param3','param4','param5','param6','param7','param8','param9','param10','param11','param12')

select m.number, 'status1', 'Red', datediff(day, (select max(datechanged) from statushistory where accountid = m.number), getdate())
from master m with (nolock)
inner join customer c with (nolock) on m.customer = c.customer
where m.status = 'status1'
and datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) >= 3
and qlevel < 998
and (m.desk not like 'ATY%')
and (isnull(m.link,0) = 0 or m.linkdriver = 1)
and m.desk not in ('param1','param2','param3','param4','param5','param6','param7','param8','param9','param10','param11','param12')
select m.number, 'status1', 
CASE 
    WHEN datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) <= 1 THEN 'Green'
    WHEN datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) between 1 and 3 THEN 'Yellow'
    WHEN datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) > 3 THEN 'Red'
END
, datediff(day, (select max(datechanged) from statushistory where accountid = m.number), getdate())
from master m with (nolock)
inner join customer c with (nolock) on m.customer = c.customer
where m.status = 'status1'
--and datediff(day, (select max(sh.datechanged) from statushistory sh where accountid = m.number), getdate()) <= 1
and qlevel < 998
and (m.desk not like 'ATY%')
and (isnull(m.link,0) = 0 or m.linkdriver = 1)
and m.desk not in ('param','param','param','param','param','param','param','param','param','param','param','param')
with cte as (
    select 
        m.number, 
        m.status, 
        days = datediff(day, (select max(datechanged) from statushistory where accountid = m.number), getdate())
    from 
        master m with (nolock)
        inner join customer c with (nolock) on m.customer = c.customer
    where 
        m.status = 'status1'
        and d between 1 and 2
        and qlevel < 998
        and (m.desk not like 'ATY%')
        and (isnull(m.link,0) = 0 or m.linkdriver = 1)
        and m.desk not in ('param1','param2','param3','param4','param5','param6','param7','param8','param9','param10','param11','param12')

), colorThresholds as (
    select color = 'Green', minDays = null, maxDays = 1 union
    select 'Yellow', 2, 3 union
    select 'Red', 4, null
)

/* now apply the color thresholds */
select
    c.*,
    ct.color
from
    cte c
    /* outer join in case the thresholds don't cover all ranges */
    left outer join colorThresholds ct on
        (ct.minDays is null or c.days >= ct.minDays)
        and (ct.maxDays is null or c.days <= ct.maxDays)