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

急SQL查询语句,单表,解决好就结分.

2012-04-22 
急...SQL查询语句,单表,在线等,解决好就结分...表:IDCODENUM120000892230007223320000891查询出结果为:200

急...SQL查询语句,单表,在线等,解决好就结分...
表:
ID             CODE                 NUM
1               2000089             2
2               3000722             3
3               2000089             1


查询出结果为:
2000089
2000089
3000722
3000722
3000722
2000089  

意思就是把CODE字段值列出NUM个数。

[解决办法]
select
a.CODE
from
表 a,
(select (select count(*) from sysobjects where id <=t.id) as NUM from sysobjects t) b
where
a.NUM> =b.NUM
order by
a.ID
[解决办法]


create table T(ID int, CODE varchar(20), NUM int)
insert T select 1, '2000089 ', 2
union all select 2, '3000722 ', 3
union all select 3, '2000089 ', 1

declare @max int
select @max=max(NUM) from T

set rowcount @max
select ID=identity(int, 1, 1) into #T from syscolumns, sysobjects
set rowcount 0


select A.* from T A, #T B
where A.NUM> =B.ID

--result
ID CODE NUM
----------- -------------------- -----------
1 2000089 2
1 2000089 2
2 3000722 3
2 3000722 3
2 3000722 3
3 2000089 1

(6 row(s) affected)

热点排行