我有一个有5列的表。该表如下所示:
桌子上的一点解释:
Group
都有6个属性。groups
具有相同的属性。例如,Group 8010
与group 8005
具有完全相同的属性。Line
列每增加一行就会增加1。 groups
,其中包含25-30个组。这是我想要完成的事情:
我正在尝试编写一个insert
语句来执行此操作:
Insert
语句,该语句从一个group
获取值并将其插入到下一个Rank()
中。 Line
但不确定我是否可以在此处使用此函数)根据以前的值自动填充INSERT INTO
TableName (Group, Line, Brand, Param, Value)
VALUES
(8010
,RANK()
,(
Select
Brand, Param, Value
from
TableName
Where
Group = '8005'
)
值。我的插入语句应该如下所示:
group
对于下一个Group
,我将编写相同的查询,但将8015
值更改为RANK()
,依此类推。
我很难搞清楚SELECT
部分和^(\D*)0+
子查询以获取要插入的值。
任何帮助将不胜感激。谢谢
粗体部分是新数据。我正在使用SQL Server 2012。
答案 0 :(得分:1)
这是一种只需更改@nextGroup
变量的简单方法。它使用ROW_NUMBER()
。
declare @table table ([Group] int, Line int, Brand char(2), [Param] varchar(64), Value varchar(256))
insert into @table values
(8005,1,'MO','CUT SPEED','55 Series'),
(8005,2,'MO','CUT BLADE','450 Series'),
(8005,3,'MO','CUT SPEED OV','60 Series'),
(8005,4,'MO','CUT BLADE OV','475 Series'),
(8005,5,'MO','CUT COMMENTS','Slow'),
(8005,6,'MO','CUT OV COMMENTS','Fast')
declare @nextGroup int = 8010
declare @currentGroup int = (select max([Group]) from @table where [Group] < @nextGroup)
declare @nextLine int = (select max([Line]) from @table where [Group] = @currentGroup)
insert into @table
select
@nextGroup as [Group]
,@nextline + row_number() over (order by (select null)) as Line
,t.Brand
,t.Param
,t.Value
from @table t
where t.[Group] = @currentGroup
set @nextGroup = 8015
set @currentGroup = (select max([Group]) from @table where [Group] < @nextGroup)
set @nextLine = (select max([Line]) from @table where [Group] = @currentGroup)
insert into @table
select
@nextGroup as [Group]
,@nextline + row_number() over (order by (select null)) as Line
,t.Brand
,t.Param
,t.Value
from @table t
where t.[Group] = @currentGroup
select * from @table
使用游标
--test data
declare @table table ([Group] int, Line int, Brand char(2), [Param] varchar(64), Value varchar(256))
insert into @table values
(8005,1,'MO','CUT SPEED','55 Series'),
(8005,2,'MO','CUT BLADE','450 Series'),
(8005,3,'MO','CUT SPEED OV','60 Series'),
(8005,4,'MO','CUT BLADE OV','475 Series'),
(8005,5,'MO','CUT COMMENTS','Slow'),
(8005,6,'MO','CUT OV COMMENTS','Fast')
--variables for handling data conditions
declare @nextGroup int
declare @currentGroup int
declare @nextLine int
--table with all the group numbers to use
declare @groupTable table (nextGroup int)
insert into @groupTable values
(8010),
(8015),
(8020)
--cursor to loop through the set based inserts
declare groupCursor cursor fast_forward for
select nextGroup from @groupTable
open groupCursor
fetch next from groupCursor into @nextGroup
while @@FETCH_STATUS = 0
begin
--get the last group inserted. The where condition isn't really needed
set @currentGroup = (select max([Group]) from @table where [Group] < @nextGroup)
--this figures out what the last line number was in the table
set @nextLine = (select max([Line]) from @table where [Group] = @currentGroup)
insert into @table
select
@nextGroup as [Group]
--Here we increase the line number by 1 according to the last one
,@nextLine + row_number() over (order by (select null)) as Line
,t.Brand
,t.Param
,t.Value
from @table t
where t.[Group] = @currentGroup
fetch next from groupCursor into @nextGroup
end
close groupCursor
deallocate groupCursor
select * from @table