SQL Server:dense_rank()

发布于 2024-12-05 07:09:45 字数 769 浏览 1 评论 0原文

我有一个表A: (ID int,batch int,new_batch int)

ID和batch已经填充:

ID  Batch New_Batch
1   01    NULL
2   01    NULL
3   02    NULL
4   02    NULL
5   02    NULL
6   03    NULL
7   04    NULL
8   05    NULL

现在我想根据以下select语句填充New_batch。

(select batch from tableA where id in (3,8))
  1. 现在对于这个 select 语句,我们得到batch = 02 和batch =5。现在我想分配new_batch,以便首先对select语句的结果进行排序(batch02然后batch05),其余数据应按批处理的递增顺序而不是在select语句中排序。 (第1、3、4批) 结果应该是:

      ID Batch New_Batch
       1   01    03
       2   01    03
       3   02    01
       4   02    01
       5   02    01
       6   03    04
       7   04    05
       8   05    02

谢谢。 PS:可以使用DENSE_RANK(),请不要硬编码!

I have a tableA:
(ID int, batch int, new_batch int)

ID and batch are populated already:

ID  Batch New_Batch
1   01    NULL
2   01    NULL
3   02    NULL
4   02    NULL
5   02    NULL
6   03    NULL
7   04    NULL
8   05    NULL

Now I want to populate New_batch according to the following select statement.

(select batch from tableA where id in (3,8))
  1. now for this select statement, we get batch = 02 and batch =5. now I want to assign new_batch such that the result of select statement should be ordered first (batch02 then batch05) and the remaining data should be ordered in increasing order of batch NOT in select statement. (batch 1,3,4)
    result should be:

      ID Batch New_Batch
       1   01    03
       2   01    03
       3   02    01
       4   02    01
       5   02    01
       6   03    04
       7   04    05
       8   05    02

Thanks.
PS: DENSE_RANK() can be used, and please dont hard-code !!

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

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

发布评论

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

评论(2

情释 2024-12-12 07:09:45
/*Please provide DDL like the below for future questions*/
DECLARE @tableA TABLE
(
ID INT PRIMARY KEY,
Batch INT,
New_Batch INT NULL
)

INSERT INTO @tableA(ID,Batch)
SELECT 1,1 UNION ALL
SELECT 2,1 UNION ALL   
SELECT 3,2 UNION ALL   
SELECT 4,2 UNION ALL   
SELECT 5,2 UNION ALL   
SELECT 6,3 UNION ALL   
SELECT 7,4 UNION ALL   
SELECT 8,5   

/*Answer*/
;WITH T
     AS (SELECT *,
                DENSE_RANK() OVER (ORDER BY 
                      CASE
                        WHEN Batch IN (SELECT Batch
                                       FROM   @tableA
                                       WHERE  ID IN ( 3, 8 )) 
                         THEN 0 
                         ELSE 1 END, Batch) AS NB
         FROM   @tableA)
UPDATE T
SET    New_Batch = NB

SELECT *
FROM   @tableA  
/*Please provide DDL like the below for future questions*/
DECLARE @tableA TABLE
(
ID INT PRIMARY KEY,
Batch INT,
New_Batch INT NULL
)

INSERT INTO @tableA(ID,Batch)
SELECT 1,1 UNION ALL
SELECT 2,1 UNION ALL   
SELECT 3,2 UNION ALL   
SELECT 4,2 UNION ALL   
SELECT 5,2 UNION ALL   
SELECT 6,3 UNION ALL   
SELECT 7,4 UNION ALL   
SELECT 8,5   

/*Answer*/
;WITH T
     AS (SELECT *,
                DENSE_RANK() OVER (ORDER BY 
                      CASE
                        WHEN Batch IN (SELECT Batch
                                       FROM   @tableA
                                       WHERE  ID IN ( 3, 8 )) 
                         THEN 0 
                         ELSE 1 END, Batch) AS NB
         FROM   @tableA)
UPDATE T
SET    New_Batch = NB

SELECT *
FROM   @tableA  
囚你心 2024-12-12 07:09:45
declare @T table
(
  ID int,
  Batch char(2),
  New_Batch char(2)
)

insert into @T values
(1,   '01',    NULL),
(2,   '01',    NULL),
(3,   '02',    NULL),
(4,   '02',    NULL),
(5,   '02',    NULL),
(6,   '03',    NULL),
(7,   '04',    NULL),
(8,   '05',    NULL)

;with C as
(
  select T1.New_Batch,
         dense_rank() over(order by -T2.ID desc, T1.Batch) as rn
  from @T as T1
    left outer join (select Batch, ID 
                     from @T
                     where ID in (3, 8)) as T2
      on T1.Batch = T2.Batch 
)
update C 
set New_Batch = right(100+rn, 2)


select *
from @T
order by ID

在这里尝试:

declare @T table
(
  ID int,
  Batch char(2),
  New_Batch char(2)
)

insert into @T values
(1,   '01',    NULL),
(2,   '01',    NULL),
(3,   '02',    NULL),
(4,   '02',    NULL),
(5,   '02',    NULL),
(6,   '03',    NULL),
(7,   '04',    NULL),
(8,   '05',    NULL)

;with C as
(
  select T1.New_Batch,
         dense_rank() over(order by -T2.ID desc, T1.Batch) as rn
  from @T as T1
    left outer join (select Batch, ID 
                     from @T
                     where ID in (3, 8)) as T2
      on T1.Batch = T2.Batch 
)
update C 
set New_Batch = right(100+rn, 2)


select *
from @T
order by ID

Try here : https://data.stackexchange.com/stackoverflow/q/113031/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文