读书人

怎么解决字段重复的记录

发布时间: 2012-06-14 16:00:31 作者: rapoo

如何解决字段重复的记录
SQL得到如下记录集。如何让NA重复的记录中用NULL值表示?谢谢!

NA CJ KM
--------------------
a 2 a2
a 1 a1
a 3 a3
b 1 b1
b 3 b3
b 2 b2
b 4 b4
b 5 b5

想得到以下格式:

NA CJ KM
--------------------
a 2 a2
null 1 a1
null 3 a3
b 1 b1
null 3 b3
null 2 b2
null 4 b4
null 5 b5



[解决办法]
用程序
[解决办法]

SQL code
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([NA] varchar(1),[CJ] int,[KM] varchar(2))insert [tb]select 'a',2,'a2' union allselect 'a',1,'a1' union allselect 'a',3,'a3' union allselect 'b',1,'b1' union allselect 'b',3,'b3' union allselect 'b',2,'b2' union allselect 'b',4,'b4' union allselect 'b',5,'b5'goselect case when rn=1 then na end as na,cj,kmfrom(select rn=row_number() over(partition by na order by getdate()),* from tb)t/**na   cj          km---- ----------- ----a    2           a2NULL 1           a1NULL 3           a3b    1           b1NULL 3           b3NULL 2           b2NULL 4           b4NULL 5           b5(8 行受影响)**/
[解决办法]
探讨

SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([NA] varchar(1),[CJ] int,[KM] varchar(2))
insert [tb]
select 'a',2,'a2' union all
select 'a',1,'a1' union all
select 'a……

[解决办法]
探讨
引用:

SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([NA] varchar(1),[CJ] int,[KM] varchar(2))
insert [tb]
select 'a',2,'a2' union all
select 'a',1,'a1' ……

读书人网 >SQL Server

热点推荐