计算每个学生的分数。两列的计算相互依赖
calculation of points for each student. two columns' calculations are based each other
我想使用 cte 或任何其他方法计算 sql 服务器中的剩余点数
计算公式:见附图。
总分 100
我已经使用 cte 为每个学生使用了行计数。
Row_no |st_id |plenty |weekBonus |weekbenformula |remaining_Points
1 |1 |5 |0 |0 |95
2 |1 |3 |2 |2 |94
3 |1 |2 |0 |0 |92
4 |1 |3 |2 |6 |95
5 |1 |1 |1 |1 |95
6 |1 |3 |1 |1 |93
7 |1 |2 |0 |0 |91
Excel附上示例图片。
第一行计算 st_id 1: 100 -5 +0= 95
st_id1 的第二行计算:95 - 3 +2=94 这里我必须取之前剩余的点数。等等...
或此问题的任何其他解决方案。
这可以通过递归 cte 来完成,如下所示。 weekbenformula
& remaining_points
的计算是按照公式
declare @tbl table
(
row_no int,
st_id int,
plenty int,
weekbonus int,
weekbenformula int,
remaining_points int
)
insert into @tbl (row_no, st_id, plenty, weekbonus)
values (1, 1, 5, 0),
(2, 1, 3, 2),
(3, 1, 2, 0),
(4, 1, 3, 2),
(5, 1, 1, 1),
(6, 1, 3, 1),
(7, 1, 2, 0);
with rcte as
(
select row_no, st_id, plenty, weekbonus,
weekbenformula = 0,
remaining_points = 95
from @tbl
where row_no = 1
union all
select t.row_no, t.st_id, t.plenty, t.weekbonus,
weekbenformula = case when r.remaining_points >= 95 then 1 else 3 end
* t.weekbonus,
remaining_points = r.remaining_points - t.plenty
+ (case when r.remaining_points >= 95 then 1 else 3 end * t.weekbonus)
from rcte r
inner join @tbl t on r.row_no = t.row_no - 1
)
select *
from rcte
我想使用 cte 或任何其他方法计算 sql 服务器中的剩余点数
计算公式:见附图。 总分 100 我已经使用 cte 为每个学生使用了行计数。
Row_no |st_id |plenty |weekBonus |weekbenformula |remaining_Points
1 |1 |5 |0 |0 |95
2 |1 |3 |2 |2 |94
3 |1 |2 |0 |0 |92
4 |1 |3 |2 |6 |95
5 |1 |1 |1 |1 |95
6 |1 |3 |1 |1 |93
7 |1 |2 |0 |0 |91
Excel附上示例图片。
第一行计算 st_id 1: 100 -5 +0= 95
st_id1 的第二行计算:95 - 3 +2=94 这里我必须取之前剩余的点数。等等...
或此问题的任何其他解决方案。
这可以通过递归 cte 来完成,如下所示。 weekbenformula
& remaining_points
的计算是按照公式
declare @tbl table
(
row_no int,
st_id int,
plenty int,
weekbonus int,
weekbenformula int,
remaining_points int
)
insert into @tbl (row_no, st_id, plenty, weekbonus)
values (1, 1, 5, 0),
(2, 1, 3, 2),
(3, 1, 2, 0),
(4, 1, 3, 2),
(5, 1, 1, 1),
(6, 1, 3, 1),
(7, 1, 2, 0);
with rcte as
(
select row_no, st_id, plenty, weekbonus,
weekbenformula = 0,
remaining_points = 95
from @tbl
where row_no = 1
union all
select t.row_no, t.st_id, t.plenty, t.weekbonus,
weekbenformula = case when r.remaining_points >= 95 then 1 else 3 end
* t.weekbonus,
remaining_points = r.remaining_points - t.plenty
+ (case when r.remaining_points >= 95 then 1 else 3 end * t.weekbonus)
from rcte r
inner join @tbl t on r.row_no = t.row_no - 1
)
select *
from rcte