增加雪花中现有的序列

发布于 2025-01-22 12:07:54 字数 1200 浏览 2 评论 0原文

我正在尝试增加使用雪花中序列生成的现有ID字段。

参考,这是代码&示例输出:

代码

CREATE OR REPLACE SEQUENCE id_sequence
START WITH 1
INCREMENT BY 1
;

输出

Id   Keyword    URL    Type              Position_Group 
1     A          A      Ad                1
2     A          B      Ad                2             
3     A          C      Organic           1           
4     A          D      Organic           2          
5     A          E      Organic           3
6     A          F      Featured_Snippet  1           

尝试添加使用序列增加此表上的新数据尚未提供我需要的所需输出)。试图使用Alter序列并使用Max(ID)作为变量以传递为序列,但到目前为止没有运气。感谢任何建议!

所需的输出

Id   Keyword    URL    Type              Position_Group 
1     A          A      Ad                1
2     A          B      Ad                2             
3     A          C      Organic           1           
4     A          D      Organic           2          
5     A          E      Organic           3
6     A          F      Featured_Snippet  1           
--
7     A          G      Organic           4
8     A          H      Organic           5

I'm trying to increment an existing ID field that was generated using sequence in snowflake.

For reference here's the code & sample output:

Code

CREATE OR REPLACE SEQUENCE id_sequence
START WITH 1
INCREMENT BY 1
;

Output

Id   Keyword    URL    Type              Position_Group 
1     A          A      Ad                1
2     A          B      Ad                2             
3     A          C      Organic           1           
4     A          D      Organic           2          
5     A          E      Organic           3
6     A          F      Featured_Snippet  1           

Trying to add new data that increments on this table using sequence has not provided the desired output that I need which to increment based off of the max(Id). Tried to use ALTER SEQUENCE and use max(Id) as a variable to pass into sequence but no luck so far. Appreciate any recommendations!

Desired Output

Id   Keyword    URL    Type              Position_Group 
1     A          A      Ad                1
2     A          B      Ad                2             
3     A          C      Organic           1           
4     A          D      Organic           2          
5     A          E      Organic           3
6     A          F      Featured_Snippet  1           
--
7     A          G      Organic           4
8     A          H      Organic           5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2025-01-29 12:07:54

我认为您正在尝试实现SCD,并且您希望替代密钥会增加1-每次插入新记录时 - 如果这是您想要的,那么我不建议您使用序列。

您应该使用“自动启动”概念。

创建或替换表Test_surogate_key(
test_id编号(38,0)未自动插入,
test_name varchar(2),
);

让我知道这是否有帮助。

I think you are trying to implement SCD and you want the surrogate keys to be incremented by 1 - each time a new record is inserted - if this is what you are looking for then I wouldn't recommend you to use sequence.

You should use "autoincrement" concept.

create or replace TABLE test_surrogate_key (
test_ID NUMBER(38,0) NOT NULL autoincrement,
test_name VARCHAR(2),
);

Let me know if this helps.

拥有 2025-01-29 12:07:54

如果您反复运行第一行,这就是为什么您总是从1开始获得ID的原因。我认为您已经说出了一个问题。.

CREATE OR REPLACE SEQUENCE id_sequence START WITH 1 INCREMENT BY 1 ;

您创建表格,如果评论出来,

--CREATE OR REPLACE TABLE serp_table AS  
WITH serp_tmp(Keyword, URL, Type, Position_Group) as (
    SELECT * from VALUES 
    ('bob', 'https://bobthebuild.com/', 'Ad', 1),
    ('A', 'https://asite.com/', 'Organic', 1),
    ('A', 'https://aasite.com/', 'Organic', 2)
)
SELECT  
    id_sequence.nextval AS Id, 
    Keyword, URL, Type, Position_Group
FROM serp_tmp;

请访问:

ID关键字URL类型position_group
1鲍勃https://bobthebuild.com/ad1
2ahttps://asite.com/有机1
3Ahttps://aasite.com/有机2

秒2次运行给出:

ID关键字URL类型position_group
4Bobhttps:// bobthebuild.com/ad1
5ahttps://asite.com/有机1
6Ahttps://aasite.com/有机2

因此,如果我们不重新创建序列以及如何使用序列以及它的工作原理。

我想我仍在努力理解您的挣扎是什么。

If you run that first line repeatedly, this is why you always get id starting at 1. Which I think you have said is a problem..

CREATE OR REPLACE SEQUENCE id_sequence START WITH 1 INCREMENT BY 1 ;

You CREATE table, if commented out to just be a SELECT

--CREATE OR REPLACE TABLE serp_table AS  
WITH serp_tmp(Keyword, URL, Type, Position_Group) as (
    SELECT * from VALUES 
    ('bob', 'https://bobthebuild.com/', 'Ad', 1),
    ('A', 'https://asite.com/', 'Organic', 1),
    ('A', 'https://aasite.com/', 'Organic', 2)
)
SELECT  
    id_sequence.nextval AS Id, 
    Keyword, URL, Type, Position_Group
FROM serp_tmp;

gives:

IDKEYWORDURLTYPEPOSITION_GROUP
1bobhttps://bobthebuild.com/Ad1
2Ahttps://asite.com/Organic1
3Ahttps://aasite.com/Organic2

second run gives:

IDKEYWORDURLTYPEPOSITION_GROUP
4bobhttps://bobthebuild.com/Ad1
5Ahttps://asite.com/Organic1
6Ahttps://aasite.com/Organic2

So if we don't recreate the sequence and how it's used it works how I would expect.

I guess I am still struggling to understand what your struggle is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文