在 SQL 中加入运行总和
Join Runing Sum in TSQL
我想加入一个 运行 总和直到特定的时间点。例如。我有两张桌子
Table一个
TimestampOfInterest
2001-01-01
2001-02-01
2001-03-01
Table B
Timestamp Credits
2001-01-01 1
2001-01-05 1
2001-02-10 1
2001-03-15 1
加入 B -> A 应该导致
TimestampOfInterest Credits
2001-01-01 0
2001-02-01 2
2001-03-01 3
这是给定 TimestampOfInterest 之前的学分总和。
有人可以帮忙吗?
拉兹鲁
不确定您是否需要加入。你可以简单地这样做:
Select TimestampOfInterest,
(Select SUM(Credits)
from TableB
where Timestamp < A.TimeStampOfInterest and Category = A.Category) Credits
From TableA A
我想加入一个 运行 总和直到特定的时间点。例如。我有两张桌子
Table一个
TimestampOfInterest
2001-01-01
2001-02-01
2001-03-01
Table B
Timestamp Credits
2001-01-01 1
2001-01-05 1
2001-02-10 1
2001-03-15 1
加入 B -> A 应该导致
TimestampOfInterest Credits
2001-01-01 0
2001-02-01 2
2001-03-01 3
这是给定 TimestampOfInterest 之前的学分总和。
有人可以帮忙吗? 拉兹鲁
不确定您是否需要加入。你可以简单地这样做:
Select TimestampOfInterest,
(Select SUM(Credits)
from TableB
where Timestamp < A.TimeStampOfInterest and Category = A.Category) Credits
From TableA A