带sum的递归连接

xt0899hw  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(284)

我有以下格式的数据:

FromStateID ToStateID Seconds
1           2         10
2           3         20
3           4         15
4           5         5

我需要以下输出

FromStateID ToStateID Seconds
1           2         10
2           3         20
3           4         15
4           5         5
1           3         10+20
1           4         10+20+15
1           5         10+20+15+5
2           4         20+15
2           5         20+15+5
3           5         15+5

此输出显示所用的总时间 FromStateIdToStateId 按时间顺序排列。
请帮忙。

m2xkgtsf

m2xkgtsf1#

我认为这是一个递归的cte,它遵循以下链接:

with cte as (
      select FromStateID, ToStateID, Seconds
      from t
      union all
      select cte.FromStateId, t.ToStateId, cte.Seconds + t.Seconds
      from cte join
           t
           on cte.toStateId = t.FromStateId
     )
select *
from cte;

这是一把小提琴。

4ktjp1zp

4ktjp1zp2#

@gordon linoff是更好的解决方案。下面是实现同样目标的另一种选择。
您可以使用交叉连接和分组方式来实现这一点

DECLARE @table table(FromStateId int, ToStateId int, seconds int)

insert into @table 
values
(1           ,2         ,10),
(2           ,3         ,20),
(3           ,4         ,15),
(4           ,5         ,5 );

;with cte_fromToCombination as
(select f.fromStateId, t.tostateId
from
(select distinct fromStateId from @table) as f
cross join
(select distinct toStateId from @table) as t
)
select c.FromStateId, c.ToStateId, t.sumseconds as Total_seconds 
from cte_fromToCombination as c
CROSS APPLY 
(SELECT sum(t.seconds)
from
@table as t
WHERE t.ToStateId <= c.ToStateId
) as t(sumseconds)
where c.tostateId > c.fromStateId
order by FromStateId,ToStateId
+-------------+-----------+---------------+
| FromStateId | ToStateId | Total_seconds |
+-------------+-----------+---------------+
|           1 |         2 |            10 |
|           1 |         3 |            30 |
|           1 |         4 |            45 |
|           1 |         5 |            50 |
|           2 |         3 |            30 |
|           2 |         4 |            45 |
|           2 |         5 |            50 |
|           3 |         4 |            45 |
|           3 |         5 |            50 |
|           4 |         5 |            50 |
+-------------+-----------+---------------+

相关问题