Sql 相同列值不同行到不同列值不同行

Sql 相同列值不同行到不同列值不同行,sql,sql-server,Sql,Sql Server,假设我有一张像下面这样的桌子 Column a Column b ------------------------------------------ aaaa 13 aaaa 22 aaaa 3 aaaa

假设我有一张像下面这样的桌子

Column a                         Column b
------------------------------------------
aaaa                              13     
aaaa                              22     
aaaa                               3     
aaaa                              23    
bbbb                              44    
bbbb                              56    
bbbb                               9     
bbbb                               0   
我想从一个表中获取数据并将其放入另一个表中,但我想使列的属性唯一,因为它不是唯一的

我正在尝试提出SQL语句

DECLARE @Count INT 
SET @count = 4

case when @count <= 4 then
while @Count = @Count - 1 and @Count <> 0
select @Count + columnA, columnBform tablename

您可以使用
行编号()


这将通过升序
列b
在共享sahme
列a
的每组记录中分配越来越多的编号。如果对您更为相关,您可以使用另一列对记录进行排序。

您可以使用窗口功能
行号
进行排序

SELECT CONCAT(ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY Col1), Col1) Col1,
       Col2
FROM
(
  VALUES
  ('aaaa',                              13),     
  ('aaaa',                              22),     
  ('aaaa',                              3 ),    
  ('aaaa',                              23),    
  ('bbbb',                              44),    
  ('bbbb',                              56),    
  ('bbbb',                              9 ),    
  ('bbbb',                              0 )
) T(Col1, Col2)

您看过
行号()了吗
SELECT
    CAST(ROW_NUMBER() OVER(PARTITION BY column_a ORDER BY column_b) AS VARCHAR(5))
        + column_a,
    column_b
FROM mytable
SELECT CONCAT(ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY Col1), Col1) Col1,
       Col2
FROM
(
  VALUES
  ('aaaa',                              13),     
  ('aaaa',                              22),     
  ('aaaa',                              3 ),    
  ('aaaa',                              23),    
  ('bbbb',                              44),    
  ('bbbb',                              56),    
  ('bbbb',                              9 ),    
  ('bbbb',                              0 )
) T(Col1, Col2)