22,209
社区成员
发帖
与我相关
我的任务
分享
--按分数降序
--建立表
create table tb(ID int,名称 varchar(10),分数 int)
insert tb
select 1, '小丽', 15
union all select 2, '小明', 20
union all select 3, '小强', 20
union all select 4, '小张', 10
union all select 5, '小闯', 15
--sql 2005 SQL
select 排名=dense_rank() over(order by 分数 desc),名称,分数 from tb
drop table tb
--结果
/*
排名 名称 分数
-------------------- ---------- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
*/
----2000的
create table tb(ID int , 名称 varchar(10),分数 int)
go
insert into tb select
1 ,'小丽', 15 union all select
2 ,'小明', 20 union all select
3 ,'小强', 20 union all select
4 ,'小张',10 union all select
5 ,'小闯', 15
select (select count(distinct 分数) from tb where 分数>=a.分数) as 排名,名称,分数
from tb a order by 排名
排名 名称 分数
----------- ---------- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
--drop table tb
--建立表
create table tb(ID int,名称 varchar(10),分数 int)
insert tb
select 1, '小丽', 15
union all select 2, '小明', 20
union all select 3, '小强', 20
union all select 4, '小张', 10
union all select 5, '小闯', 15
--sql 2005 SQL
select 排名=dense_rank() over(order by 分数),名称,分数 from tb
drop table tb
--结果
/*
排名 名称 分数
-------------------- ---------- -----------
1 小张 10
2 小闯 15
2 小丽 15
3 小明 20
3 小强 20
*/
select 排名=dense_rank() over(order by 分数),名称,分数 from table
--2000
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([ID] INT,[名称] NVARCHAR(10),[分数] INT)
INSERT [tb]
SELECT 1,N'小丽',15 UNION ALL
SELECT 2,N'小明',20 UNION ALL
SELECT 3,N'小强',20 UNION ALL
SELECT 4,N'小张',10 UNION ALL
SELECT 5,N'小闯',15
GO
select (select count(distinct 分数) from tb where 分数>=a.分数) as 排名,名称,分数
from tb a
--> 生成测试数据表:tb
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([ID] INT,[名称] NVARCHAR(10),[分数] INT)
INSERT [tb]
SELECT 1,N'小丽',15 UNION ALL
SELECT 2,N'小明',20 UNION ALL
SELECT 3,N'小强',20 UNION ALL
SELECT 4,N'小张',10 UNION ALL
SELECT 5,N'小闯',15
GO
--SELECT * FROM [tb]
-->SQL查询如下:
--2000
select id=(select count(distinct [分数])+1 from tb where [分数]>a.[分数]),[名称],[分数]from [tb] a order by id
/*
排名 名称 分数
-------------------- ---------- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
*/
SQL2000要这样--2000
select id=(select count(distinct [分数])+1 from tb where [分数]>a.[分数]),[名称],[分数]from [tb] a order by id
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-03-31 09:49:50
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4053.00 (Intel X86)
-- May 26 2009 14:24:20
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[名称] varchar(4),[分数] int)
insert [tb]
select 1,'小丽',15 union all
select 2,'小明',20 union all
select 3,'小强',20 union all
select 4,'小张',10 union all
select 5,'小闯',15
--------------开始查询--------------------------
select
排名=(select count(distinct 分数 )+1 from tb where t.分数<分数),
名称,分数
from
tb t
order by
1
----------------结果----------------------------
/* 排名 名称 分数
----------- ---- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
*/
create table tb(ID int , 名称 varchar(10),分数 int)
go
insert into tb select
1 ,'小丽', 15 union all select
2 ,'小明', 20 union all select
3 ,'小强', 20 union all select
4 ,'小张',10 union all select
5 ,'小闯', 15
select dense_rank() over(order by [分数] desc) as 排名,名称,分数
from tb
排名 名称 分数
-------------------- ---------- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
--drop table tb
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[名称] varchar(4),[分数] int)
insert [tb]
select 1,'小丽',15 union all
select 2,'小明',20 union all
select 3,'小强',20 union all
select 4,'小张',10 union all
select 5,'小闯',15
--2005
select id=dense_rank() over(order by[分数] desc ) ,[名称],[分数]from [tb] order by id
--2000
select id=(select count(1)+1 from tb where [分数]>a.[分数]),[名称],[分数]from [tb] a order by id
/*
id 名称 分数
----------- ---- -----------
1 小明 20
1 小强 20
3 小丽 15
3 小闯 15
5 小张 10
(5 行受影响)
*
/
create table tb(ID int , 名称 varchar(10),分数 int)
go
insert into tb select
1 ,'小丽', 15 union all select
2 ,'小明', 20 union all select
3 ,'小强', 20 union all select
4 ,'小张',10 union all select
5 ,'小闯', 15
select dense_rank() over(order by [分数]) as 排名,名称,分数
from tb
排名 名称 分数
-------------------- ---------- -----------
1 小张 10
2 小闯 15
2 小丽 15
3 小明 20
3 小强 20
(5 行受影响)
--drop table tb
dense_rank 记错了--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-03-31 09:47:57
-- Version:Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)
-- Mar 29 2009 10:27:29
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
--------------------------------------------------------------------------
--> 生成测试数据表:tb
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([ID] INT,[名称] NVARCHAR(10),[分数] INT)
INSERT [tb]
SELECT 1,N'小丽',15 UNION ALL
SELECT 2,N'小明',20 UNION ALL
SELECT 3,N'小强',20 UNION ALL
SELECT 4,N'小张',10 UNION ALL
SELECT 5,N'小闯',15
GO
--SELECT * FROM [tb]
-->SQL查询如下:
SELECT 排名=DENSE_RANK()OVER(ORDER BY [分数] DESC),
名称,分数
FROM TB
/*
排名 名称 分数
-------------------- ---------- -----------
1 小明 20
1 小强 20
2 小丽 15
2 小闯 15
3 小张 10
(5 行受影响)
*/