更新以获取 check_order

发布于 2024-10-17 01:16:33 字数 493 浏览 3 评论 0原文

我有一个包含值的表,

col1     col2     col3 
1         0         ABA
1         0         ABB
1         0         ABC
2         0         BBA
2         0         BBB
2         0         BBC

我试图更新该表以查看 col1 的重复次数,在本例中 col1 已重复 3 次,因此对 col2 的每次更新都会增加 1。

更新表后所需的输出

col1     col2     col3 
1         1         ABA
1         2         ABB
1         3         ABC
2         1         BBA
2         2         BBB
2         3         BBC

I have a table with values,

col1     col2     col3 
1         0         ABA
1         0         ABB
1         0         ABC
2         0         BBA
2         0         BBB
2         0         BBC

I am trying to update the table to see the number of repetition of col1, in this case col1 has repeated 3 times so each update to col2 incremented by 1.

Required output after the update table

col1     col2     col3 
1         1         ABA
1         2         ABB
1         3         ABC
2         1         BBA
2         2         BBB
2         3         BBC

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

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

发布评论

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

评论(2

夏末的微笑 2024-10-24 01:16:34

假设您打算将 col3 按非降序排列,则应该这样做:

UPDATE MyTable
SET col2=(SELECT COUNT(*) 
          FROM MyTable AS T2 
          WHERE T2.col1=T1.col1 AND T2.col3<=T1.col3)
FROM MyTable AS T1

如果 col3 中对于特定的 col1 值存在重复项,您将在 col2 中获得重复项。

如果您感兴趣,这里有一个使用排名函数的相当冗长(并且执行成本更高)的解决方案。对于 col1/col3 中的重复项,它具有与之前的解决方案相同的问题(即计数重复):

UPDATE MyTable
SET col2=(
     -- In the following query, DISTINCT merges rank dups caused by col3 dups
     -- SELECT TOP(1) MyRank would also work.
     SELECT DISTINCT MyRank 
     FROM (
      SELECT col3,
              DENSE_RANK() OVER (PARTITION BY col1 ORDER BY col3) AS MyRank
      FROM MyTable
      WHERE col1=UpdatedTable.col1
     ) As RankTable
     WHERE RankTable.col3=UpdatedTable.col3)
FROM MyTable AS UpdatedTable

Assuming you are intending col3 to be in non-descending order, this should do it:

UPDATE MyTable
SET col2=(SELECT COUNT(*) 
          FROM MyTable AS T2 
          WHERE T2.col1=T1.col1 AND T2.col3<=T1.col3)
FROM MyTable AS T1

You will get duplicates in col2, if there are duplicates in col3 for a particular col1 value.

In case you are interested, here is a pretty verbose (and more expensive execution wise) solution using a ranking function. It has the same issue (i.e., the count gets repeated) for duplicates in col1/col3, as the previous solution:

UPDATE MyTable
SET col2=(
     -- In the following query, DISTINCT merges rank dups caused by col3 dups
     -- SELECT TOP(1) MyRank would also work.
     SELECT DISTINCT MyRank 
     FROM (
      SELECT col3,
              DENSE_RANK() OVER (PARTITION BY col1 ORDER BY col3) AS MyRank
      FROM MyTable
      WHERE col1=UpdatedTable.col1
     ) As RankTable
     WHERE RankTable.col3=UpdatedTable.col3)
FROM MyTable AS UpdatedTable
浅浅淡淡 2024-10-24 01:16:33

一个简单的 row_number() 应该可以工作

;with TMP as (
select *, row_number() over (partition by col1 order by col3) as RowNum
from tbl
)
update TMP set col2=RowNum

其中

  • tbl:是您的表名称
  • partition by col1:重置每个col1组的行编号
  • order by col3:是 col1 组内编号的基础

A simple row_number() -ing should work

;with TMP as (
select *, row_number() over (partition by col1 order by col3) as RowNum
from tbl
)
update TMP set col2=RowNum

Where

  • tbl: is your table name
  • partition by col1: resets the row numbering for each col1 group
  • order by col3: is the basis for numbering within a col1 group
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文