计算并划分行数

时间:2017-07-26 18:56:44

标签: sql-server tsql

我有以下数据集

1, Nike
2, Adidas 
3, oasis
4, reebok
5, puma
6, airjordan
每两行

将它们影响到A,依此类推

A, 1, Nike
A, 2, Adidas
B, 3, oasis
B, 4, reebok
C, 5, puma
C, 6, airjordan

1 个答案:

答案 0 :(得分:1)

我相信这就是你要找的东西:

您需要使用DENSE_RANK()

示例数据:

DECLARE @Shoes TABLE ( ShoesId INT, Brand VARCHAR(10))

INSERT INTO @Shoes
VALUES
(1, 'Nike'),
(2, 'Adidas' ),
(3, 'oasis'),
(4, 'reebok'),
(5, 'puma'),
(6, 'airjordan')

查询:

SELECT CHAR(ASCII('A') + DENSE_RANK() OVER ( ORDER BY (ShoesId%2) + ShoesId -1) - 1), 
      * 
FROM @Shoes

结果:

enter image description here