某字段重复的记录,如何查找出来后在值前面加个标识
表A
f1,f2,f3
100,1,1
100,2,2
100,3,3
200,4,4
200,5,5
300,6,6
执行结果
A100,1,1
B100,2,2
C100,3,3
A200,4,4
B200,5,5
300,6,6
问题点数:100、回复次数:7Top
1 楼fcuandy(了此残生.)回复于 2006-07-02 11:45:29 得分 10
有兩個問題想問一下
1,樓主所說的值重復是指三個字段都重復,還是僅f1重復?
2,如果最後一條記錄,即 300,6,6後面還有一條 100,7,7
那麼,這條記錄前輟是不是為D? (或者請說明不可能出現這樣的結果,因為記錄是按順序的)
另外,f2,f3的值是不是連續的?Top
2 楼zljqf(秋风)回复于 2006-07-02 11:50:23 得分 0
重复是指的只有f1重复,重复的记录都按字母顺序编号
f1,f2,f3
100,1,1
100,2,2
100,3,3
200,4,4
200,5,5
300,6,6
100,7,7
执行结果
A100,1,1
B100,2,2
C100,3,3
D100,7,7
A200,4,4
B200,5,5
300,6,6
Top
3 楼zljqf(秋风)回复于 2006-07-02 11:51:34 得分 0
f2,f3可以不考虑,不是连续的Top
4 楼LouisXIV(夜游神)回复于 2006-07-02 11:52:54 得分 90
declare @tableA table
(
f1 varchar(10),
f2 int,
f3 int
)
insert into @tableA
select '100',1,1 union all
select '100',2,2 union all
select '100',3,3 union all
select '200',4,4 union all
select '200',5,5 union all
select '300',6,6
select id=identity(int,1,1),* into # from @tableA
select case when (select count(1) from # where f1=a.f1 group by f1)>1 then char(65+isnull((select count(1) from # where f1=a.f1 and id<a.id group by f1),0))+rtrim(f1) else f1 end as f1,
f2,f3
from # a
go
drop table #
/*
f1 f2 f3
----------- ----------- -----------
A100 1 1
B100 2 2
C100 3 3
A200 4 4
B200 5 5
300 6 6
*/Top
5 楼zljqf(秋风)回复于 2006-07-02 12:04:48 得分 0
高!
感谢Top
6 楼fcuandy(了此残生.)回复于 2006-07-02 12:06:36 得分 0
DECLARE @t TABLE(f1 int,f2 int,f3 int)
INSERT @t
SELECT 100,1,1
UNION ALL SELECT 100,2,2
UNION ALL SELECT 100,3,3
UNION ALL SELECT 200,4,4
UNION ALL SELECT 200,5,5
UNION ALL SELECT 300,6,6
UNION ALL SELECT 100,6,6
SELECT *,IDENTITY(int) id INTO #t FROM @t ORDER BY f1
SELECT c=char(65+(SELECT COUNT(1) FROM #t b WHERE b.f1=a.f1 AND b.id<a.id))+RTRIM(f1),f2,f3 FROM #t a
DROP TABLE #tTop
7 楼fcuandy(了此残生.)回复于 2006-07-02 12:07:16 得分 0
剛才去回了個VB內存出錯的貼子,來遲了.Top




