【问题标题】:Update table with 3 repeating values, 1 unique value, and 1 incremental value with a query SQL Server使用查询 SQL Server 使用 3 个重复值、1 个唯一值和 1 个增量值更新表
【发布时间】:2017-03-08 19:09:01
【问题描述】:

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

桌上的一点解释:

  1. 每个Group 有6 个属性。
  2. 大约有 25-30 个 groups 具有相同的属性。例如,Group 8010group 8005 具有完全相同的属性。
  3. 唯一的区别是,Line 每新行增加 1 列。
  4. 我有 7 或 8 个不同的 groups,其中包含 25-30 个组。

这是我想要完成的任务:
我正在尝试编写一个 insert 语句来执行此操作:

  1. 使用Insert 语句从一个group 获取值并将其插入下一个。
  2. 使用基于先前值自动填充Line 值的函数(我查看了Rank(),但不确定是否可以在此处使用)。

我的插入语句应该是这样的:

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 子查询以获取要插入的值。

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

编辑 1:
这是新表的样子:

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

【问题讨论】:

  • 这是否意味着Lines 组8010 应该是7 - 12
  • 是的。而line8015 应该是13-18

标签: sql-server sql-insert


【解决方案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

【讨论】:

  • 那太完美了!我只是不明白@nextLine 变量是如何工作的。在第一次运行期间,Max(Line) 将是 6。这句话中Line 的值是多少:Line + @NextLine?从 1 到 6 是如何变化的?
  • 好吧,我将要更改 @CrazyCucumber 以使其更具活力...给我一点时间
  • 好的 @CrazyCucumber 检查一下,如果它没有意义,请告诉我,我会详细说明。您可以删除光标并手动设置参数,它会以相同的方式工作(与仅在第一次通过的第一个代码相比,这次正确且准确)
  • 修复了原来的帖子,现在它可以工作了,多遍。并回答你的问题。它需要 6 并将其添加到 1 用于第一行,将其添加到 2 用于第二行,等等......但这仅适用于单个更新。 row_number 解决了这个问题。
  • 那太美了。非常感谢。
猜你喜欢
  • 2010-10-16
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2020-12-26
  • 2023-02-04
  • 2021-11-12
相关资源
最近更新 更多