如何添加包含一组重复记录的第一个不同记录的ID值的列

发布于 2025-02-05 03:30:25 字数 1358 浏览 2 评论 0原文

我有下表名为EMP

IDfnamelname
1MihirSheth
2MitDasondi
3NarraHarsha
4MihirSheth
5ShreyMalvi
6MitDasondi
7UjasPatel

我想更改表EMP并创建一个新的专栏,并为重复的记录进行了新的纪录它将具有第一次出现不同记录的ID的值。

对于不同的记录,该值将在newid列中为null。通过“第一次出现不同记录”,我的意思是根据ID列进行排序时首先出现的记录。

为了更好地理解,请参阅以下内容。

idnewidfnamelname
1nullmihirsheth
2nullmitdasondi
3nullNarraHarsha
41MihirSheth
5NullShreyMalvi
62MitDasondi
7nullUjasPatel,

请告知我如何继续我,还要记住我如何继续记住新专栏在实际表中,而不是在CTE或其他临时表中。

I have the following table named emp

idfnamelname
1MihirSheth
2MitDasondi
3NarraHarsha
4MihirSheth
5ShreyMalvi
6MitDasondi
7UjasPatel

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.

idNewIdfnamelname
1nullMihirSheth
2nullMitDasondi
3nullNarraHarsha
41MihirSheth
5nullShreyMalvi
62MitDasondi
7nullUjasPatel

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 技术交流群。

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

发布评论

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

评论(2

南冥有猫 2025-02-12 03:30:26

您首先需要将列添加到emp schema:

ALTER TABLE emp ADD NewId INT;

然后,您可以为每个员工使用最小ID的选择,然后更新emp表格,其中ID不通信最小ID:

UPDATE     emp 
INNER JOIN (SELECT fname, lname, MIN(id) AS id
            FROM emp
            GROUP BY fname, lname) min_emps
        ON emp.fname = min_emps.fname
       AND emp.lname = min_emps.lname
       AND emp.id   <> min_emps.id
SET emp.NewId = min_emps.id;

这是mysql demo ,尽管这可能在Sybase中起作用也。


edit :给定 sybase文档在更新语句上,有关问题的相应更新语句可能是以下内容:

UPDATE emp 
SET emp.NewId = min_emps.id;
FROM emp 
JOIN (SELECT fname, lname, MIN(id) AS id  
      FROM emp
      GROUP BY fname, lname             ) min_emps
  ON emp.fname = min_emps.fname
 AND emp.lname = min_emps.lname
 AND emp.id   <> min_emps.id

You need first to add a column to the emp schema:

ALTER TABLE emp ADD NewId INT;

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:

UPDATE     emp 
INNER JOIN (SELECT fname, lname, MIN(id) AS id
            FROM emp
            GROUP BY fname, lname) min_emps
        ON emp.fname = min_emps.fname
       AND emp.lname = min_emps.lname
       AND emp.id   <> min_emps.id
SET emp.NewId = min_emps.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:

UPDATE emp 
SET emp.NewId = min_emps.id;
FROM emp 
JOIN (SELECT fname, lname, MIN(id) AS id  
      FROM emp
      GROUP BY fname, lname             ) min_emps
  ON emp.fname = min_emps.fname
 AND emp.lname = min_emps.lname
 AND emp.id   <> min_emps.id
痴情 2025-02-12 03:30:25

我们可以尝试在此处使用min作为分析功能:

SELECT id, CASE WHEN id <> MIN(id) OVER (PARTITION BY fname, lname)
                THEN MIN(id) OVER (PARTITION BY fname, lname) END AS NewId,
       fname, lname
FROM emp
ORDER BY id;

We can try using MIN as an analytic function here:

SELECT id, CASE WHEN id <> MIN(id) OVER (PARTITION BY fname, lname)
                THEN MIN(id) OVER (PARTITION BY fname, lname) END AS NewId,
       fname, lname
FROM emp
ORDER BY id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文