在Sql Server 2008中
我的选择查询结果如下所示。
Col1 Col2 Col3
------------------------------------------
1001 HO 160.00
1001 HO 40.00
1001 HO 200.00
1002 HO 10.00
1002 HO 130.00
1003 HO 10.00
1003 HO 130.00
1003 HO 130.00
1003 HO 230.00
现在,我想要选择结果如下(请在Col2中找到更改)
Col1 Col2 Col3
------------------------------------------
1001 HO1 160.00
1001 HO2 40.00
1001 HO3 200.00
1002 HO1 10.00
1002 HO2 130.00
1003 HO1 10.00
1003 HO2 130.00
1003 HO3 130.00
1003 HO4 230.00
那边的专家......请帮我提取上面的数据。
答案 0 :(得分:2)
我刚刚创建了示例数据,并为您提供了查询。因此,您可以在原始查询中实现
declare @t table(id int,Name varchar(10))
insert into @t (id,name)values (1,'HO')
insert into @t (id,name)values (1,'HO')
insert into @t (id,name)values (2,'HO')
insert into @t (id,name)values (2,'HO')
select id,name +CAST(row_number()OVER(PARTITION BY id order by name )AS VARCHAR)rn from @t
答案 1 :(得分:1)
试试这个
select col1,
col2+cast(row_number() over (partition by col1 order by col1) as varchar(10)) as col2,
col3 from your_table
答案 2 :(得分:0)
SELECT Col1,
Col2 + CAST([Row] AS VARCHAR(10)) Col2,
Col3
FROM (
SELECT *,ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) [Row]
FROM [Table]) LU
这也可以帮到你。