Sql server 2008 SQL Server 2008中的CTE:如何递归计算小计

Sql server 2008 SQL Server 2008中的CTE:如何递归计算小计,sql-server-2008,recursion,add,aggregate,common-table-expression,Sql Server 2008,Recursion,Add,Aggregate,Common Table Expression,我有一个表格,其中汽车的某些部件是按层次关系关联的,我还有每一行中制造这些部件的成本。这是表格的简化: parentId Id description qty manufacturingCost costDescripcion -------- --- ----------- --- ----------------- --------------- NULL 1 Car 1 100 Assembly the car NULL

我有一个表格,其中汽车的某些部件是按层次关系关联的,我还有每一行中制造这些部件的成本。这是表格的简化:

parentId  Id description qty manufacturingCost costDescripcion
-------- --- ----------- --- ----------------- ---------------
NULL      1  Car          1  100               Assembly the car
NULL      2  Motorcycle   1  100               Assembly the motrocycle
 1       11  Wheel        4   20               Assembly the wheel
11      111  Rim          1   50               Manufacture the rim
11      112  Tire         1   60               Manufacture the tire
 1       12  Door+Window  4   30               Assembly the door and the window
12      121  Door         1   30               Manufacture the door
12      122  Window       2   10               Manufacture the window
 2       11  Wheel        2   15               Assembly the wheel
CREATE TABLE #Costs 
(
  parentId int, 
  Id int, 
  description varchar(50),
  qty int, 
  manufacturingCost int,
  costDescripcion varchar(150)
)

INSERT INTO #Costs VALUES (NULL , 1, 'Car', 1, 100, 'Assembly the car')
INSERT INTO #Costs VALUES (NULL , 2, 'Motorcycle', 1, 100, 'Assembly the motrocycle')
INSERT INTO #Costs VALUES (1 , 11, 'Wheel', 4, 20, 'Assembly the wheel')
INSERT INTO #Costs VALUES (11 , 111, 'Rim', 1, 50, 'Manufacture the rim')
INSERT INTO #Costs VALUES (11 , 112, 'Tire', 1, 60, 'Manufacture the tire')
INSERT INTO #Costs VALUES (1 , 12, 'Door+Window', 4, 30, 'Assembly the door and the window')
INSERT INTO #Costs VALUES (12 , 121, 'Door', 1, 30, 'Manufacture the door')
INSERT INTO #Costs VALUES (12 , 122, 'Window', 2, 10, 'Manufacture the window')
INSERT INTO #Costs VALUES (2 , 11, 'Wheel', 2, 15, 'Assembly the wheel')
我需要把整个家谱从“Car”开始,显示每个分支的总数量和总成本。更好的解释:一辆车有四个轮子,每个轮子有一个轮辋和一个轮胎,所以我应该有一辆车,四个轮子,四个轮胎,四个轮辋。成本稍微复杂一点:组装一辆车要花100美元,但我还要加上这个成本,组装4个车轮(4x20)和制造4个轮辋(4x50)和4个轮胎(4x60)的成本,以及门窗的成本

这是预期的结果:

parentId  Id description qty manufacturingCost   recLevel
-------- --- ----------- --- -----------------   ---------------
NULL       1  Car          1 940 (100+4*130+4*80) 0
 1        11  Wheel        4 130 (20+50+60)       1
 1        12  Door+Window  4 80  (30+30+2*10)     1
12       121  Door         4 30                   2
12       122  Window       8 10                   2
11       111  Rim          4 50                   2
11       112  Tire         4 60                   2
我可以很容易地使用递归函数或存储过程来实现这一点,但是对于更复杂的结构来说,速度非常慢,所以我尝试使用公共表表达式来实现这一点。但是我没有找到计算成本的方法。我使用一个递归的CTE,从顶层开始,向下,我得到数量的总和,但是我应该从内部到外部,在结构中求出成本的总和,我该怎么做呢

以下是创建表的代码:

parentId  Id description qty manufacturingCost costDescripcion
-------- --- ----------- --- ----------------- ---------------
NULL      1  Car          1  100               Assembly the car
NULL      2  Motorcycle   1  100               Assembly the motrocycle
 1       11  Wheel        4   20               Assembly the wheel
11      111  Rim          1   50               Manufacture the rim
11      112  Tire         1   60               Manufacture the tire
 1       12  Door+Window  4   30               Assembly the door and the window
12      121  Door         1   30               Manufacture the door
12      122  Window       2   10               Manufacture the window
 2       11  Wheel        2   15               Assembly the wheel
CREATE TABLE #Costs 
(
  parentId int, 
  Id int, 
  description varchar(50),
  qty int, 
  manufacturingCost int,
  costDescripcion varchar(150)
)

INSERT INTO #Costs VALUES (NULL , 1, 'Car', 1, 100, 'Assembly the car')
INSERT INTO #Costs VALUES (NULL , 2, 'Motorcycle', 1, 100, 'Assembly the motrocycle')
INSERT INTO #Costs VALUES (1 , 11, 'Wheel', 4, 20, 'Assembly the wheel')
INSERT INTO #Costs VALUES (11 , 111, 'Rim', 1, 50, 'Manufacture the rim')
INSERT INTO #Costs VALUES (11 , 112, 'Tire', 1, 60, 'Manufacture the tire')
INSERT INTO #Costs VALUES (1 , 12, 'Door+Window', 4, 30, 'Assembly the door and the window')
INSERT INTO #Costs VALUES (12 , 121, 'Door', 1, 30, 'Manufacture the door')
INSERT INTO #Costs VALUES (12 , 122, 'Window', 2, 10, 'Manufacture the window')
INSERT INTO #Costs VALUES (2 , 11, 'Wheel', 2, 15, 'Assembly the wheel')
这是我写的CTE:

with CTE(parentId, id, description, totalQty, manufacturingCost, recLevel)
as
(
  select c.parentId, c.id, c.description, c.qty, c.manufacturingCost, 0
  from #Costs c
  where c.id = 1

  union all

  select c.parentId, c.id, c.description, c.qty * ct.totalQty, c.manufacturingCost, ct.recLevel + 1
  from #Costs c
  inner join CTE ct on ct.id = c.parentId 
)
select * from CTE
这是我得到的结果,正如你所看到的,不是预期的结果(成本没有增加):

可以用CTE做我想做的事情吗?如果是,我怎么做

多谢各位


安图

你可以试试这样的东西

DECLARE @Table TABLE(
        parentId INT,
        Id INT,
        description VARCHAR(50),
        qty FLOAT,
        manufacturingCost FLOAT,
        costDescripcion VARCHAR(50)
)

INSERT INTO @Table SELECT NULL,1,'Car',1,100,'Assembly the car' 
INSERT INTO @Table SELECT NULL,2,'Motorcycle',1,100,'Assembly the motrocycle' 
INSERT INTO @Table SELECT 1,11,'Wheel',4,20,'Assembly the wheel' 
INSERT INTO @Table SELECT 11,111,'Rim',1,50,'Manufacture the rim' 
INSERT INTO @Table SELECT 11,112,'Tire',1,60,'Manufacture the tire' 
INSERT INTO @Table SELECT 1,12,'Door+Window',4,30,'Assembly the door and the window' 
INSERT INTO @Table SELECT 12,121,'Door',1,30,'Manufacture the door' 
INSERT INTO @Table SELECT 12,122,'Window',2,10,'Manufacture the window' 
INSERT INTO @Table SELECT 2,11,'Wheel',2,15,'Assembly the wheel'

;WITH Vals AS (
        SELECT  *,
                qty Level_Qty,
                CAST(id AS VARCHAR(MAX)) + '\' AS [LEVEL]
        FROM    @Table
        WHERE   parentId IS NULL
        UNION ALL
        SELECT  t.*,                
                p.qty * t.qty Level_Qty,
                CAST(p.[LEVEL] AS VARCHAR(MAX))  + CAST(t.id AS VARCHAR(MAX)) + '\' AS [LEVEL]
        FROM    @Table t INNER JOIN
                Vals p  ON  p.Id = t.parentId
)
SELECT  *,
        (SELECT SUM(Level_Qty * manufacturingCost) FROM Vals WHERE [Level] LIKE v.[LEVEL] + '%') / Level_Qty
FROM    Vals v
ORDER BY [LEVEL]

非常感谢astander,它很有效!我一直在考虑改变CTE,但没有注意到解决方案可能是这样的。