首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

第一次提问,各位大大多担待,该如何处理

2012-03-09 
第一次提问,各位大大多担待tab1:(一个字段)tab2AAB10010020070822-00110010020070822-001211变成这样子211

第一次提问,各位大大多担待
tab1:(一个字段)                               tab2
    A                                                   A                     B
  100                                               100               20070822-001
  100                                               100               20070822-001
  211             变成这样子               211               20070822-002
  211                 --------》           211               20070822-002
  211                                               211               20070822-002  
  123                                               123               20070822-003
  123                                               123               20070822-003
    .                                                   .                     .
    .                                                   .                     .
    .                                                   .                     .
解释一下,就是新生成一列(B)
B字段是   今天的日期+‘-’+数字     ),只要A字段发生一次变化,后面的数字就+1
我是设个变量每次比较的,觉得不好。请高手指点一下,tab1也比较大,就认为有几十万行好了,最好效率高一点,谢谢。


[解决办法]
看看这个对你有没有用?


sqlserver 有如下数据

ID
0001
0001
0002
0002
0001
0001

实现效果

ID
0001
0002
0001

只有相邻的数据进行筛选!怎么处理啊,谢谢大家了!


declare @a table( ID varchar(5))
insert into @a
select '0001 ' union all
select '0001 ' union all
select '0002 ' union all
select '0002 ' union all
select '0001 ' union all
select '0001 '

select identity(int,1,1) rn,* into #0 from @a



select * from #0 a
where not exists ( select 1 from #0 b where b.ID=a.ID and b.rn=a.rn-1)


rn ID
----------- -----
1 0001
3 0002
5 0001

(所影响的行数为 3 行)
[解决办法]
--见下边例子
--建表
create table tb(ID nvarchar(20),col nvarchar(12))
go

--函数
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

alter function f_getID(@uID nvarchar(12))
returns nchar(12)
as
begin
declare @count int,@ID int,@Num nchar(13)
select @ID = 0,@Num = ' '
select @count = count(1) from tb where ID = @uID
if @count > 0
select @Num = (select top 1 col from tb where ID = @uID)
else begin
declare @Nowdate nvarchar(8),@OtherDate nvarchar(8)
select @Nowdate = convert(char(8),getdate(),112),@OtherDate = '19990101 '
select @OtherDate = substring(Col,1,8),@ID = right(Col,3) from tb where substring(Col,1,8) = @Nowdate
if @OtherDate <> @Nowdate
begin
select @ID = 0
end;
select @Num = @Nowdate + '- ' + right((1000 + @ID + 1),3)
end
return @Num
end


--测试
declare @s nvarchar(12)
set @s = '101 '
insert into tb
select @s,dbo.f_getID(@s)

--查看
select * from tb
[解决办法]
请楼主看看这个,不知道效率如何:
create table #A(a varchar(3),B varchar(5),c int)
INSERT INTO #A
SELECT '100 ', 'oh ',32 UNION ALL
SELECT '100 ', 'my ',31 UNION ALL
SELECT '201 ', 'god ',33 UNION ALL
SELECT '201 ', 'god ',43 UNION ALL
SELECT '201 ', 'bless ',23 UNION ALL
SELECT '333 ', 'me ',43 UNION ALL
SELECT '333 ', '! ',21 UNION ALL
SELECT '324 ', 'Over ',35

select distinct(a) INTO #C FROM #A
select IDENTITY(int, 1,1) as NO,a INTO #D from #C

SELECT #A.a,#A.b,#A.c c,convert(varchar(10),getdate(),120) + '- '+LEFT( '000 '+CAST(#D.NO AS VARCHAR),4) AS d FROM #A LEFT JOIN #D ON #A.a=#D.a ORDER BY #A.a

DROP table #A,#C,#D

执行结果:
100oh322007-08-22-0001
100my312007-08-22-0001
201god332007-08-22-0002
201god432007-08-22-0002
201bless232007-08-22-0002
324Over352007-08-22-0003
333me432007-08-22-0004
333!212007-08-22-0004

热点排行