值X到Y的SQL增量字段
我试图在@start和@end两个变量之间,通过@increment来获取一个字段,在@end处重置,然后在@start处重新开始,并继续表中的所有行 如果 那么我要找的是:值X到Y的SQL增量字段,sql,sql-server,Sql,Sql Server,我试图在@start和@end两个变量之间,通过@increment来获取一个字段,在@end处重置,然后在@start处重新开始,并继续表中的所有行 如果 那么我要找的是: 1 .06 2 .08 3 .10 4 .06 5 .08 6 .10 我尝试过使用带模的Row_Number(),但在第一次重置后就不起作用了 您可以使用递归CTE: with cte as ( select 1 as n, cast(@start as decimal(10, 2)) as val
1 .06
2 .08
3 .10
4 .06
5 .08
6 .10
我尝试过使用带模的Row_Number(),但在第一次重置后就不起作用了 您可以使用递归CTE:
with cte as (
select 1 as n, cast(@start as decimal(10, 2)) as val
union all
select n + 1, cast(val + @increment as decimal(10, 2))
from cte
where val + @increment <= @end
)
select *
from cte cross join
t;
子查询计算序列号和grp的大小。外部查询对行号进行分区,然后进行算术运算
基本上相同的想法可以做到如下:
select t.*,
(@start + @increment * (row_number() over (order by id) - 1) % cast(1 + round((@end - @start) / @increment, 0) as int))
from t;
但是这个算法有点难以理解。我想您正在寻找一种解决方案来更新表中的字段。您可以使用一个变量来执行此操作,如下所示 您需要将更新间隔写入
WHERE
条件
DECLARE @Tbl TABLE (Id INT, TempColumn FLOAT)
INSERT INTO @Tbl (Id)
VALUES
(1),(2),(3),(4)
DECLARE @Inc FLOAT = 0
UPDATE @Tbl
SET TempColumn = @Inc,
@Inc += 0.2
SELECT * FROM @Tbl
结果:
Id TempColumn
----------- --------------
1 0,2
2 0,4
3 0,6
4 0,8
使用递归cte根据需要生成数字(表中的行数)。然后使用表达式在每3行之后重置序列
declare @start decimal(10,2)=0.06;
declare @end decimal(10,2)=0.1;
declare @increment decimal(10,2)=0.02;
with numbers(rnum) as (select 1
union all
select rnum+1
from numbers where rnum<100 )
select (rnum-1)%3*@increment+@start as col
from numbers
我经常使用表值函数来创建动态范围。通常比递归cte更快,并且具有参数驱动的附加好处,并且容易合并到子查询、交叉应用甚至交叉连接中,如下图所示: 示例
Declare @start float = .06
Declare @end float = .10
Declare @increment float = .02
Declare @groups int = 3
Select ID = Row_Number() over (Order by A.RetSeq)
,Value = B.RetVal
From [dbo].[udf-Range-Number](1,@groups,1) A
Cross Apply [dbo].[udf-Range-Number](@start,@end,@increment) B
Order By 1,2
返回
ID Value
1 0.06
2 0.08
3 0.10
4 0.06
5 0.08
6 0.10
7 0.06
8 0.08
9 0.10
有兴趣的UDF
CREATE FUNCTION [dbo].[udf-Range-Number] (@R1 money,@R2 money,@Incr money)
Returns Table
Return (
with cte0(M) As (Select cast((@R2-@R1)/@Incr as int)),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a,cte1 b,cte1 c,cte1 d,cte1 e,cte1 f,cte1 g,cte1 h )
Select RetSeq=1,RetVal=@R1 Union All Select N+1,(N*@Incr)+@R1
From cte2
)
-- Max 100 million observations
-- Select * from [dbo].[udf-Range-Number](0,4,0.25)
您必须生成多少这样的行?请使用行发布您尝试的代码_number@vkp行数取决于表中可变的其他字段。它基本上只需要继续,直到像Row_Number()那样结束。。这不会生成超过3行。
Declare @start float = .06
Declare @end float = .10
Declare @increment float = .02
Declare @groups int = 3
Select ID = Row_Number() over (Order by A.RetSeq)
,Value = B.RetVal
From [dbo].[udf-Range-Number](1,@groups,1) A
Cross Apply [dbo].[udf-Range-Number](@start,@end,@increment) B
Order By 1,2
ID Value
1 0.06
2 0.08
3 0.10
4 0.06
5 0.08
6 0.10
7 0.06
8 0.08
9 0.10
CREATE FUNCTION [dbo].[udf-Range-Number] (@R1 money,@R2 money,@Incr money)
Returns Table
Return (
with cte0(M) As (Select cast((@R2-@R1)/@Incr as int)),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a,cte1 b,cte1 c,cte1 d,cte1 e,cte1 f,cte1 g,cte1 h )
Select RetSeq=1,RetVal=@R1 Union All Select N+1,(N*@Incr)+@R1
From cte2
)
-- Max 100 million observations
-- Select * from [dbo].[udf-Range-Number](0,4,0.25)