使用查询SQL Server更新包含3个重复值,1个唯一值和1个增量值的表

时间:2017-03-08 19:09:02

标签: sql-server sql-insert

我有一个有5列的表。该表如下所示:

enter image description here

桌子上的一点解释:

  1. 每个Group都有6个属性。
  2. 大约有25-30 groups具有相同的属性。例如,Group 8010group 8005具有完全相同的属性。
  3. 唯一不同的是,Line列每增加一行就会增加1。
  4. 我有7个或8个不同的groups,其中包含25-30个组。
  5. 这是我想要完成的事情:
    我正在尝试编写一个insert语句来执行此操作:

    1. 使用Insert语句,该语句从一个group获取值并将其插入到下一个Rank()中。
    2. 使用一个函数(我查看Line但不确定我是否可以在此处使用此函数)根据以前的值自动填充INSERT INTO TableName (Group, Line, Brand, Param, Value) VALUES (8010 ,RANK() ,( Select Brand, Param, Value from TableName Where Group = '8005' ) 值。
    3. 我的插入语句应该如下所示:

      group

      对于下一个Group,我将编写相同的查询,但将8015值更改为RANK(),依此类推。

      我很难搞清楚SELECT部分和^(\D*)0+ 子查询以获取要插入的值。

      任何帮助将不胜感激。谢谢

      编辑1:
      这就是新表的样子:
      enter image description here

      粗体部分是新数据。我正在使用SQL Server 2012。

1 个答案:

答案 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