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

row_number( ) sql 2000,该如何解决

2013-11-29 
row_number( )sql 2000 通过 row_number(parttion by order by) 查询结果 如下 在sql 2000 如何实现[解决

row_number( ) sql 2000
 通过 row_number(parttion by order by) 
查询结果 如下 
在sql 2000 如何实现 
row_number( )   sql 2000,该如何解决 

[解决办法]

----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-11-27 16:43:40
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--Dec 28 2012 20:23:12 
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([a] varchar(4),[b] varchar(1),[c] int)
insert [huang]
select '广东','a',1 union all
select '广东','b',3 union all
select '广东','c',10 union all
select '广西','a',5 union all
select '中国','f',9 union all
select '中国','g',8
--------------开始查询--------------------------

select *,(SELECT COUNT(1) FROM huang b WHERE huang.a=b.a AND huang.b>=b.b)
from [huang]
----------------结果----------------------------
/* 
a    b    c           
---- ---- ----------- -----------
广东   a    1           1
广东   b    3           2
广东   c    10          3
广西   a    5           1
中国   f    9           1
中国   g    8           2
*/

[解决办法]


select id=identity(int,1,1),* into #tb from tb
select *,(select count(1)+1 from #tb where 项目=t.项目 and id<t.id) from #tb t

[解决办法]

create table an
(项目 varchar(10),产 varchar(10),价 int)

insert into an
 select '广东','a',1 union all
 select '广东','b',3 union all
 select '广东','c',10 union all
 select '广西','a',5 union all
 select '中国','f',9 union all
 select '中国','g',8


select identity(int,1,1) 'rn',项目,产,价
 into #t
 from an

select 项目,产,价,
       (select count(1) from #t b 
        where b.rn<=a.rn and b.项目=a.项目) 'rn'
 from #t a

/*
项目         产          价           rn
---------- ---------- ----------- -----------
广东         a          1           1
广东         b          3           2
广东         c          10          3
广西         a          5           1
中国         f          9           1
中国         g          8           2



(6 row(s) affected)
*/

热点排行