如何使用CTE获取递归记录?

时间:2017-12-13 14:25:12

标签: sql sql-server recursion

我正在撰写此查询:

with CTE as
(
SELECT testCategory, testType, compareTestCategory,A.MEDIAN, A.average from 
tInputValue_SamplesRequired

)

Select testCategory,testType,C.Median,C.Average,C.Median + D.Median as change,C.Average + D.average as [%change],C.compareTestCat from CTE C
left outer CTE D
on C.testCategory = D.compareTestCategory
and C.testType = D.testType

我想获得递归记录,如下图: enter image description here

但是我得到的结果如下: enter image description here

右边,如果比较测试类别等于测试类别,我想用中位数和平均值来计算。

0 个答案:

没有答案