sql - 使用 SQL 移动计算
问题描述
我需要使用移动计算来计算一个新列。
例如,我有一张桌子:
一个 | 乙 |
---|---|
10 | 15 |
11 | 14 |
12 | 13 |
我需要计算第一个值计算的新列,第二5000/10*15
个值是(5000 / 10 * 15) / 11 * 14
,第三个值是((5000 / 10 * 15) / 11 * 14) / 12 * 13
等等。其中 5000 是一个随机值,以后我会将它用作存储过程中的参数。
我知道,例如在 Excel 中,我们可以参考之前计算的单元格。如何使用 SQL 计算?
谢谢!
解决方案
create table #test (A int,B int)
insert into #test values(10,15),(11,14),(12,13)
declare @seed int=5000;
;with temp as (
select A,B,row_number() over(order by a) rn from #test
),
cte as
(
select @seed/A*B calculated,rn from temp where rn=1
union all
select c. calculated/t.A*t.B,t.rn from temp t
join cte c on t.rn=c.rn+1
)
select * from cte
推荐阅读
- php - PHPBrew:无法安装或添加 argon2i 散列
- android - 如何在 Android 中通过 IR blaster 传输十六进制代码?
- excel - 将我的 Access 表导出到 Excel,但将列中的不同值拆分到不同的工作表中
- sql - 连接表,选择所有但重命名几个 SQL BQ
- laravel - 处理会话过期,Laravel 与 Vue 和 Axios
- javascript - 数据被过滤chartJs时如何保持圆角?
- android - 是否有可能将 try catch 函数作为中缀函数?
- apache-spark - 问:数据集
平面图到空数据集 Spark Java
- list - Flutter:方法在列表中创建问题的地方
- html - 如何使用 xslt 缩进列表的 html 列表?