第一次提问,各位大大多担待

lost_queen 2007-08-22 02:39:35
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也比较大,就认为有几十万行好了,最好效率高一点,谢谢。
...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lost_queen 2007-08-22
  • 打赏
  • 举报
回复
谢谢aqbeyond(海阔天空)
用两个#表,我怎么没有想到呢~赞,虽然这样会很慢,不过合适我这个初学的
谢谢simonhehe(收购猩猩)
函数我不太会用,不过这样做应该效率高
谢谢了
给一样的分,不知道公平不,哈哈,结贴了
lost_queen 2007-08-22
  • 打赏
  • 举报
回复
恩恩
学习学习~
谢谢各位了~
第一次提问,第一次给分~
挺好玩的~!
simonhehe 2007-08-22
  • 打赏
  • 举报
回复
--建表
--drop table tab1
create table tab1(A nvarchar(10),B nvarchar(12))
insert into tab1(A)
select '100' union all
select '100' union all
select '211' union all
select '211' union all
select '211' union all
select '123' union all
select '123'
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 tab1 where A = @uID
if @count > 0
select @Num = (select top 1 B from tab1 where A = @uID)
if (@Num is null) or @count <= 0
begin
declare @Nowdate nvarchar(8),@OtherDate nvarchar(8)
select @Nowdate = convert(char(8),getdate(),112),@OtherDate = '19990101'
select @OtherDate = substring(B,1,8),@ID = right(B,3) from tab1 where substring(B,1,8) = @Nowdate
if @OtherDate <> @Nowdate
begin
select @ID = 0
end;
select @Num = @Nowdate + '-' + right((1000 + @ID + 1),3)
end;
return @Num
end

----这个函数只适合插入和分组更新时使用
--测试1
insert into tab1 select '212',dbo.f_getID('212')
--测试2
update tab1
set B = dbo.f_getID(A)
where a = '211'

--查看
select * from tab1
aqbeyond 2007-08-22
  • 打赏
  • 举报
回复
请楼主看看这个,不知道效率如何:
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

执行结果:
100 oh 32 2007-08-22-0001
100 my 31 2007-08-22-0001
201 god 33 2007-08-22-0002
201 god 43 2007-08-22-0002
201 bless 23 2007-08-22-0002
324 Over 35 2007-08-22-0003
333 me 43 2007-08-22-0004
333 ! 21 2007-08-22-0004
lost_queen 2007-08-22
  • 打赏
  • 举报
回复
谢谢乌龟大哥
这样合并以后添加 B 字段我会的
可是 事实上 tab1 中的每一行都要改不允许合并的~唉
说明如下:(要的是C字段了)
tab1:(一个字段) tab2
A B A B C
100 oh 100 oh 20070822-001
100 my 100 my 20070822-001
211 god 变成这样子 211 god 20070822-002
211 god --------》 211 god 20070822-002
211 bless 211 bless 20070822-002
123 me 123 me 20070822-003
123 ! 123 ! 20070822-003
. . . . .
. . . . .
. . . . .
simonhehe 2007-08-22
  • 打赏
  • 举报
回复
--见下边例子
--建表
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
dawugui 2007-08-22
  • 打赏
  • 举报
回复
看看这个对你有没有用?


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 行)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧