一个SQL小问题 ,请大神解惑谢谢

teemoJ 2016-06-18 02:44:59



如图,A列是已有数据, B列是想实现数据。 我想知道如果根据SQL做出这样的效果。谢谢、。
...全文
189 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
teemoJ 2016-06-21
  • 打赏
  • 举报
回复
谢谢各位大神帮忙 已根据3L的语句成功了。 谢谢
nvllin1992 2016-06-20
  • 打赏
  • 举报
回复
诶呀 怎么少了一个图 这个是表中的数据


这个语句我通常用于 重复数据只取一条结果
nvllin1992 2016-06-20
  • 打赏
  • 举报
回复
你瞅瞅这个
卖水果的net 2016-06-20
  • 打赏
  • 举报
回复
没有行数限制,基本上不超过 2 的 32 次方就可以的;
teemoJ 2016-06-20
  • 打赏
  • 举报
回复
引用 3 楼 ap0405140 的回复:
[quote=引用 1 楼 teemoJ 的回复:] A,和B列都存在一个表C里, 数据类型都是int。 A列就是现在这样的 B列现在是NULL值 想让变成上面这样。

create table 表C(列A int,列B int)

insert into 表C(列A)
  select 1 union all
  select 2 union all
  select 3 union all
  select 3 union all
  select 3 union all
  select 4 union all
  select 4 union all
  select 5 union all
  select 6 union all
  select 7 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 9


update a 
  set a.列B=b.colB
  from (select 列A,列B,rn=row_number() over(order by getdate()) from 表C) a
  inner join (select 列A,
                              rn=row_number() over(order by getdate()),
                              colB=row_number() over(partition by 列A order by getdate())
                     from 表C) b on a.rn=b.rn and a.列A=b.列A

select * from 表C

/*
列A          列B
----------- -----------
1           1
2           1
3           1
3           2
3           3
4           1
4           2
5           1
6           1
7           1
8           1
8           2
8           3
8           4
9           1

(15 row(s) affected)
*/
[/quote ]我的这个列A只是显示其中de一部分 这个列有上千条后期还会一直增加, 所以这样是不是会有行数限制呢
卖水果的net 2016-06-19
  • 打赏
  • 举报
回复
原列 A 中的值,下一行的数据,会不会比上一行的要小? 如果有这种情况,你想怎么处理?
唐诗三百首 2016-06-18
  • 打赏
  • 举报
回复
引用 1 楼 teemoJ 的回复:
A,和B列都存在一个表C里, 数据类型都是int。 A列就是现在这样的 B列现在是NULL值 想让变成上面这样。

create table 表C(列A int,列B int)

insert into 表C(列A)
  select 1 union all
  select 2 union all
  select 3 union all
  select 3 union all
  select 3 union all
  select 4 union all
  select 4 union all
  select 5 union all
  select 6 union all
  select 7 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 9


update a 
  set a.列B=b.colB
  from (select 列A,列B,rn=row_number() over(order by getdate()) from 表C) a
  inner join (select 列A,
                              rn=row_number() over(order by getdate()),
                              colB=row_number() over(partition by 列A order by getdate())
                     from 表C) b on a.rn=b.rn and a.列A=b.列A

select * from 表C

/*
列A          列B
----------- -----------
1           1
2           1
3           1
3           2
3           3
4           1
4           2
5           1
6           1
7           1
8           1
8           2
8           3
8           4
9           1

(15 row(s) affected)
*/
唐诗三百首 2016-06-18
  • 打赏
  • 举报
回复
用row_number()实现,

create table teem(原列A int)

insert into teem
  select 1 union all
  select 2 union all
  select 3 union all
  select 3 union all
  select 3 union all
  select 4 union all
  select 4 union all
  select 5 union all
  select 6 union all
  select 7 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 8 union all
  select 9


select 原列A,
           rn=row_number() over(partition by 原列A order by getdate()) 
 from teem

/*
原列A         rn
----------- --------------------
1           1
2           1
3           1
3           2
3           3
4           1
4           2
5           1
6           1
7           1
8           1
8           2
8           3
8           4
9           1

(15 row(s) affected)
*/
teemoJ 2016-06-18
  • 打赏
  • 举报
回复
A,和B列都存在一个表C里, 数据类型都是int。 A列就是现在这样的 B列现在是NULL值 想让变成上面这样。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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