我在[newtable]中生成我的ID时遇到问题。它必须从sub增加每个新组。 有什么建议吗? 我自己的想法是,也许我可以在嵌套选择中放入case语句? 我需要在insert语句中选择select。名为newtable的表是空的,我想用main和sub中的数据填充它。插入在这里被省略,因为它的选择需要修复。
SELECT
(SELECT NVL(MAX(newtable.ID), 0)+1 AS MAX_VAL FROM newtable)AS ID
,main.ID as ID_PLACE
, main.place
, sub.somedata
FROM main
LEFT JOIN sub on main.ID = sub.ID
某些数据看起来像这样
sub
id somedata
972 fdsfds
972 fdsfds
972 fdsfds
972 fdsfds
972 fdsfds
972 fdsfds
973 opfg
973 kl
973 uu
main
id place
972 2564
973 1122
newtable
id id_place place somedata
1 972 2564 fdsfds
1 972 2564 fdsfds
1 972 2564 fdsfds
1 972 2564 fdsfds
1 972 2564 fdsfds
1 972 2564 fdsfds
2 973 1122 opfg
2 973 1122 kl
2 973 1122 uu
答案 0 :(得分:1)
试试这个并分享结果(未经测试)
with tab as (SELECT NVL(MAX(newtable.ID), 0)+1 AS MAX_VAL FROM newtable)
SELECT ((select max_val from tab) +
dense_rank() over (order by main.ID)) ID
,main.ID as ID_PLACE
, main.place
, sub.somedata
FROM main
LEFT JOIN sub on main.ID = sub.ID
编辑: 删除了分区by子句,不是必需的。