【问题标题】:Relation between rows using a unique number使用唯一编号的行之间的关系
【发布时间】:2020-07-16 20:50:01
【问题描述】:

我正在尝试创建 4 行之间的关系。我需要这个让行有一个唯一的数字。 同样在我的下一个插入中不再给出相同的数字。

例子

ID   Name    uniqueNumber
-------------------------
 1   test1       1
 2   test2       1
 3   test3       1
 4   test4       1
 5   test1       5
 6   test2       5
 7   test3       5
 8   test4       5

我需要一些如何在同一范围内的行之间创建关系。

我想知道是否可以使用第一个插入行 ID,并在接下来的 3 中使用它。 SQL Server 有这个选项吗?

【问题讨论】:

  • 请也发布预期结果。
  • 我需要在特定行数之后插入这些行以获取一个的 id 并填充其余行。这8行会在同一个作用域之后同时插入
  • 这是您需要在插入时自动发生的事情,还是您可以稍后通过更新应用“关系”,或者您稍后需要在 select 语句中使用它?
  • 什么是“范围”?

标签: sql sql-server tsql sql-server-2012


【解决方案1】:

与其他 cmets 和答案不同,我看不出序列在这里有什么用处。如果你需要一个选择,这个查询应该可以工作。

select 
    ID,
    Name,   
    case when ID % 4 = 0 then ID -3 else (ID - ID % 4) + 1 end as UniqueNumber
from YourTable

或者您可以稍后使用以下查询更新您的表格。

update YourTable
set UniqueNumber = case when ID % 4 = 0 then ID -3 else (ID - ID % 4) + 1 end

这会产生这样的输出。

1   test1   1
2   test2   1
3   test3   1
4   test4   1
5   test1   5
6   test2   5
7   test3   5
8   test4   5
9   test1   9
10  test2   9
11  test3   9
12  test4   9
13  test1   13
14  test2   13
15  test3   13
16  test4   13

【讨论】:

    【解决方案2】:

    我不清楚你真正想做什么,但这是我的想法

    SELECT *, ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Id) % 4 UniqueNumber
    FROM
    (
      VALUES
      (1,'test1'),
      (2,'test2'),
      (3,'test3'),
      (4,'test4'),
      (5,'test1'),
      (6,'test2'),
      (7,'test3'),
      (8,'test4')
    ) T(Id, Name)
    ORDER BY Id
    

    返回:

    +----+-------+--------------+
    | Id | Name  | UniqueNumber |
    +----+-------+--------------+
    |  1 | test1 |            1 |
    |  2 | test2 |            1 |
    |  3 | test3 |            1 |
    |  4 | test4 |            1 |
    |  5 | test1 |            2 |
    |  6 | test2 |            2 |
    |  7 | test3 |            2 |
    |  8 | test4 |            2 |
    +----+-------+--------------+
    

    或者使用SEQUENCE 作为更好的方法

    CREATE SEQUENCE MySec
        AS INT   
        START WITH 1  
        INCREMENT BY 1  
        MINVALUE 1  
        MAXVALUE 4  
        CYCLE;
    
    CREATE TABLE T(
      Id INT IDENTITY(1, 1),
      Name VARCHAR(45),
      UniqueNumber  INT DEFAULT (NEXT VALUE FOR MySec)
    );
    
    INSERT T(Name)
      VALUES
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4'),
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4');
    
    SELECT * FROM T;
    

    使用计算列的更好方法

    CREATE TABLE TT(
      Id INT IDENTITY(1, 1),
      Name VARCHAR(45),
      UniqueNumber AS (Id - (Id - 1) % 4) 
    );
    
    INSERT TT(Name)
      VALUES
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4'),
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4'),
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4'),
      ('test1'),
      ('test2'),
      ('test3'),
      ('test4');
    
    SELECT * FROM TT;
    

    【讨论】:

    • 这完全不同。根据 OP Id 似乎是自动生成的可能是身份密钥,他需要生成 UniqueNumber 的逻辑,如 1、5 等。
    • @SurajKumar 正如我所说,我并不清楚。查看更新。
    猜你喜欢
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多