sql文

niuniu777 2008-03-21 11:39:08
字段和数据
code1 code2 name
0001 101 li1
0001 552 li2
0001 556 li3
0002 000 li4
0002 666 li5
0003 000 li6

我想得到No的数据:
code1 code2 name No
0001 101 li1 1
0001 552 li2 2
0001 556 li3 3
0002 000 li4 1
0002 666 li5 2
0003 000 li6 1

一条SQL 文实现,不用临时表
...全文
87 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nzperfect 2008-03-21
  • 打赏
  • 举报
回复
declare @tb table (code1 varchar(10),code2 varchar(10),name varchar(10))
insert into @tb select '0001','101','li1'
insert into @tb select '0001','552','li2'
insert into @tb select '0001','556','li3'
insert into @tb select '0002','000','li4'
insert into @tb select '0002','666','li5'
insert into @tb select '0003','000','li6'
select *,ROW_NUMBER() OVER(ORDER BY code1,code2 asc) AS 'Row Number' from @tb
dobear_0922 2008-03-21
  • 打赏
  • 举报
回复
create table tb(code1 varchar(8), code2 varchar(8), name varchar(16))
insert tb select '0001', '101', 'li1'
union all select '0001', '552', 'li2'
union all select '0001', '556', 'li3'
union all select '0002', '000', 'li4'
union all select '0002', '666', 'li5'
union all select '0003', '000', 'li6'

select t.*, [No]=(select count(1)+1 from tb where code1=t.code1 and name<t.name)
from tb t

/*
code1 code2 name No
-------- -------- ---------------- -----------
0001 101 li1 1
0001 552 li2 2
0001 556 li3 3
0002 000 li4 1
0002 666 li5 2
0003 000 li6 1

(6 row(s) affected)
*/

drop table tb
wzy_love_sly 2008-03-21
  • 打赏
  • 举报
回复
declare @tb table (code1 varchar(10),code2 varchar(10),name varchar(10))
insert into @tb select '0001','101','li1'
insert into @tb select '0001','552','li2'
insert into @tb select '0001','556','li3'
insert into @tb select '0002','000','li4'
insert into @tb select '0002','666','li5'
insert into @tb select '0003','000','li6'

select *,[no]=(select count(1) from @tb where code1=t.code1 and name<=t.name) from @tb t


code1 code2 name no
0001 101 li1 1
0001 552 li2 2
0001 556 li3 3
0002 000 li4 1
0002 666 li5 2
0003 000 li6 1
xiaomeixiang 2008-03-21
  • 打赏
  • 举报
回复
select *,no=(select count(*) from tb1 where code1=a.code1 and code2<=a.code2)
from tb1 a
wzy_love_sly 2008-03-21
  • 打赏
  • 举报
回复
select *,[no]=(select count(1) from tb where code1=t.code1 and code2<=t.code2) from tb t
kk19840210 2008-03-21
  • 打赏
  • 举报
回复

declare @tb table (code1 varchar(10),code2 varchar(10),name varchar(10))
insert into @tb select '0001','101','li1'
insert into @tb select '0001','552','li2'
insert into @tb select '0001','556','li3'
insert into @tb select '0002','000','li4'
insert into @tb select '0002','666','li5'
insert into @tb select '0003','000','li6'


select *,[No]=(select count(1) from @tb where code1=a.code1 and [name]<=a.name) from @tb a

code1 code2 name No
---------- ---------- ---------- -----------
0001 101 li1 1
0001 552 li2 2
0001 556 li3 3
0002 000 li4 1
0002 666 li5 2
0003 000 li6 1

(6 行受影响)
pt1314917 2008-03-21
  • 打赏
  • 举报
回复

select *,no=(select count(1) from tb where code1=a.code1 and code2 <=a.code2) from tb a
liuxuan123 2008-03-21
  • 打赏
  • 举报
回复


declare @tb table (code1 varchar(10),code2 varchar(10),name varchar(10))
insert into @tb select '0001','101','li1'
insert into @tb select '0001','552','li2'
insert into @tb select '0001','556','li3'
insert into @tb select '0002','000','li4'
insert into @tb select '0002','666','li5'
insert into @tb select '0003','000','li6'

select *,[no]=(select count(1) from @tb where code1=t.code1 and name<=t.name) from @tb t

27,579

社区成员

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

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