我对这个问题很感兴趣,并且我发现了解复杂查询的最佳方法是使用我自己的风格和约定重新格式化它。我将它们应用于您的解决方案,结果如下。我不知道这对你是否有任何价值......
- 有一些代码我不认为是 MS T-SQL 语法的一部分,例如
({fn xxx }
函数WEEK(xxx)
。
- 此代码可以编译,但我无法运行它,因为我没有正确配置数据表。
- 我进行了许多编码更改,这些更改需要大量解释,我将跳过其中的大部分内容。如果您想解释任何内容,请添加评论。
- 我扔了很多空白。易读和难读的代码之间的区别往往只是旁观者的感知和感受,你可能会讨厌我的约定。
- 不确定最终结果集应该是什么(即返回哪些列)
一些进一步的说明:
- 如果该周内也没有关闭任何项目,则此查询将不会获取该周内输入的项目
- 周可能是部分的,例如,并非所有 7 天都存在(将 @Interval 调整为始终包括整周——但是奇数间隔呢?)
- 将 count(*) 值乘以 1.0 以提前将它们转换为浮点数(避免强制转换和整数数学截断)
- 允许将较早的公式替换为后来的公式中的符号(此时事情变得更加清晰易读)
所以这就是我想出的:
;WITH cte as (
select
c.period
,resolved_half1
,resolved_half2
,opened_half1
,opened_half2
,row = row_number() over(order by c.yearClosed, c.weekClosed)
,y1 = ((SUM(resolved_half1) + SUM(opened_half1)) - (SUM(resolved_half2) + SUM(opened_half2))) / ((count(resolved_half1) + count(opened_half1)) / 2)
,y2 = ((SUM(resolved_half2) + SUM(opened_half2)) / (count(resolved_half2) + COUNT (opened_half2)))
,x1 = ((count(c.period)) / 4)
,x2 = (((count(c.period)) / 4) * 3)
from (select
a.yearclosed
,a.weekClosed
,a.resolved_half1
,b.yearEntered
,b.weekEntered
,b.opened_half1
,cast(a.yearClosed as varchar(5)) + ', ' + cast(a.weekClosed as varchar(5)) period
from (-- Number of items per week that closed within @Interval
select
count(distinct TicketNbr) * 1.0 resolved_half1
,datepart(wk, date_closed) weekClosed
,year(date_closed) yearClosed
from v_rpt_Service
where date_closed >= @FullInterval
group by
datepart(wk, date_closed)
,year(date_closed) ) a
left outer join (-- Number of items per week that were entered within @Interval
select
count(distinct TicketNbr) * 1.0 opened_half1
,datepart(wk, date_entered) weekEntered
,year(date_entered) yearEntered
from v_rpt_Service
where date_entered >= @FullInterval
group by
datepart(wk, date_entered)
,year(date_entered) ) b
on a.weekClosed = b.weekEntered
and a.yearClosed = b.yearEntered) c
left outer join (select
d.yearclosed
,d.weekClosed
,d.resolved_half2
,e.yearEntered
,e.weekEntered
,e.opened_half2
,cast(yearClosed as varchar(5)) + ', ' + cast(weekClosed as varchar(5)) period
from (select
count(distinct TicketNbr) * 1.0 resolved_half2
,datepart(wk, date_closed) weekClosed
,year(date_closed) yearClosed
from v_rpt_Service
where date_closed >= @HalfInterval
group by
datepart(wk, date_closed)
,year(date_closed) ) d
left outer join (select
count(distinct TicketNbr) * 1.0 opened_half2
,datepart(wk, date_entered) weekEntered
,year(date_entered) yearEntered
from v_rpt_Service
where date_entered >= @HalfInterval
group by
datepart(wk, date_entered)
,year(date_entered) ) e
on d.weekClosed = e.weekEntered
and d.yearClosed = e.yearEntered ) f
on c.period = f.period
group by
c.period
,resolved_half1
,resolved_half2
,opened_half1
,opened_half2
,c.yearClosed
,c.weekClosed
)
SELECT
row
,Period
,x1
,y1
,x2
,y2
,m = ((y1 - y2) / (x1 - x2))
,b = (y2 - (((y1 - y2) / (x1 - x2)) * x2))
,trend = ((((y1 - y2) / (x1 - x2)) * (row)) + (y2 - (((y1 - y2) / (x1 - x2)) * x2)))
from cte
order by row
作为附录,所有子查询“c”都可以替换为以下内容,而“f”则替换为稍作修改的版本。性能的好坏取决于表大小、索引和其他不可估量的因素。
select
datepart(wk, date_closed) weekClosed
,year(date_closed) yearClosed
,count (distinct case
when date_closed >= @FullInterval then TicketNbr
else null
end) resolved_half1
,count (distinct case
when date_entered >= @FullInterval then TicketNbr
else null
end) opened_half1
from v_rpt_Service
where date_closed >= @FullInterval
or date_entered >= @FullInterval
group by
datepart(wk, date_closed)
,year(date_closed)