如何添加包含一组重复记录的第一个不同记录的ID值的列
我有下表名为EMP
ID | fname | lname |
---|---|---|
1 | Mihir | Sheth |
2 | Mit | Dasondi |
3 | Narra | Harsha |
4 | Mihir | Sheth |
5 | Shrey | Malvi |
6 | Mit | Dasondi |
7 | Ujas | Patel |
我想更改表EMP并创建一个新的专栏,并为重复的记录进行了新的纪录它将具有第一次出现不同记录的ID的值。
对于不同的记录,该值将在newid列中为null。通过“第一次出现不同记录”,我的意思是根据ID列进行排序时首先出现的记录。
为了更好地理解,请参阅以下内容。
id | newid | fname | lname |
---|---|---|---|
1 | null | mihir | sheth |
2 | null | mit | dasondi |
3 | null | Narra | Harsha |
4 | 1 | Mihir | Sheth |
5 | Null | Shrey | Malvi |
6 | 2 | Mit | Dasondi |
7 | null | Ujas | Patel, |
请告知我如何继续我,还要记住我如何继续记住新专栏在实际表中,而不是在CTE或其他临时表中。
I have the following table named emp
id | fname | lname |
---|---|---|
1 | Mihir | Sheth |
2 | Mit | Dasondi |
3 | Narra | Harsha |
4 | Mihir | Sheth |
5 | Shrey | Malvi |
6 | Mit | Dasondi |
7 | Ujas | Patel |
I want to alter the table emp and create a new column NewId, such that for duplicate records it will have the value of the id of the first occurence of distinct record.
For distinct records the value will be null in the NewId column. By the "first occurence of the distinct record" I mean the record that occurs first when sorted on basis of id column.
For a better understanding, please see the following.
id | NewId | fname | lname |
---|---|---|---|
1 | null | Mihir | Sheth |
2 | null | Mit | Dasondi |
3 | null | Narra | Harsha |
4 | 1 | Mihir | Sheth |
5 | null | Shrey | Malvi |
6 | 2 | Mit | Dasondi |
7 | null | Ujas | Patel |
Please advise me on how to proceed and also keep in mind that I want the new column to be in the actual table, not in cte or other temporary table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先需要将列添加到
emp
schema:然后,您可以为每个员工使用最小ID的选择,然后更新
emp
表格,其中ID不通信最小ID:这是mysql demo ,尽管这可能在Sybase中起作用也。
edit :给定 sybase文档在更新语句上,有关问题的相应
更新
语句可能是以下内容:You need first to add a column to the
emp
schema:Then you can use a selection of the smallest ids for each employee and update the
emp
table where the id does not correspond to the minimum id:Here's a MySQL demo, though this may work in sybase too.
Edit: Given Sybase documentation on update statements, the corresponding
UPDATE
statement for your problem may be the following:我们可以尝试在此处使用
min
作为分析功能:We can try using
MIN
as an analytic function here: