el *_*smu 2 sql numbers teradata
我有一个表serviceClusters与列标识(1590值).然后我有另一个表serviceClustersNew与列ID,文本和注释.在此表中,我有一些文本和注释的值,ID始终为1.这里是表的示例:
[1,dummy1,hello1;
1,dummy2,hello2;
1,dummy3,hello3;
等等.]
我现在想要列ID中的值是表serviceClusters的连续索引加上当前行号:在我们的例子中,这将是1591,1592和1593.
我试图像这样解决问题:首先我用最大值更新了列ID,然后我尝试添加行号,但这不起作用:
-- Update ID to the maximum value 1590
UPDATE serviceClustersNew
SET ID = (SELECT MAX(ID) FROM serviceClusters);
-- This command returns the correct values 1591, 1592 and 1593
SELECT ID+ROW_NUMBER() OVER (ORDER BY Text_ID) AS RowNumber
FROM serviceClustersNew
-- But I'm not able to update the table with this command
UPDATE serviceClustersNew
SET ID = (SELECT ID+ROW_NUMBER() OVER (ORDER BY Text_ID) AS RowNumber FROM
serviceClustersNew)
Run Code Online (Sandbox Code Playgroud)
通过发送最后一个命令,我收到错误"语法错误:子查询中不允许有序分析函数.".你有什么建议吗,我怎么解决这个问题?我知道我可以使用易失性表或添加列来实现它,但有没有办法不创建新表/更改当前表?
你必须使用UPDATE FROM重写它,语法有点笨重:
UPDATE serviceClustersNew
FROM
(
SELECT text_id,
(SELECT MAX(ID) FROM serviceClusters) +
ROW_NUMBER() OVER (ORDER BY Text_ID) AS newID
FROM serviceClustersNew
) AS src
SET ID = newID
WHERE serviceClustersNew.Text_ID = src.Text_ID
Run Code Online (Sandbox Code Playgroud)