+ -
当前位置:首页 → 问答吧 → 请教sql如何按照汇总的结果来排序?

请教sql如何按照汇总的结果来排序?

时间:2011-11-19

来源:互联网

有如下格式的表,我希望人名能够按照他们总分从高到低来排列,请问 sql 如何写?

name score
张三 23
张三 78
黄五 57
黄五 68
李三 65
李三 78

作者: marklr   发布时间: 2011-11-19

SQL code

select a.*
from tb a,(select name,sum(score) score from tb group by name) b
where a.name = b.name
order by b.score desc,a.name,a.score desc

作者: AcHerat   发布时间: 2011-11-19

SQL code

if object_id('tb','U') is not null
   drop table tb
go
create table tb
(
 name varchar(10),
 score int
)
go
insert into tb
select '张三',23 union all
select '张三',78 union all
select '黄五',57 union all
select '黄五',68 union all
select '李三',65 union all
select '李三',78
go
select name,sum(score) from tb group by name order by sum(score) desc
go
/*
name       
---------- -----------
李三         143
黄五         125
张三         101

(3 行受影响)
*/

作者: pengxuan   发布时间: 2011-11-19

SQL code

create table tb
(
 name varchar(10),
 score int
)
go
insert into tb
select '张三',23 union all
select '张三',78 union all
select '黄五',57 union all
select '黄五',68 union all
select '李三',65 union all
select '李三',78
go

select a.*,b.score as sumScore
from tb a,(select name,sum(score) score from tb group by name) b
where a.name = b.name
order by b.score desc,a.name,a.score desc

drop table tb

/**************

name       score       sumScore
---------- ----------- -----------
李三         78          143
李三         65          143
黄五         68          125
黄五         57          125
张三         78          101
张三         23          101

(6 行受影响)

作者: AcHerat   发布时间: 2011-11-19

啊,不是分组排序

作者: pengxuan   发布时间: 2011-11-19

to :AcHerat

from 多个表这是叫左连接还是叫什么?

作者: marklr   发布时间: 2011-11-19

SQL code

if object_id('tb','U') is not null
   drop table tb
go
create table tb
(
 name varchar(10),
 score int
)
go
insert into tb
select '张三',23 union all
select '张三',78 union all
select '黄五',57 union all
select '黄五',68 union all
select '李三',65 union all
select '李三',78
go
select name,qty=sum(score) over(partition by name) from tb order by qty desc
go
/*
name       qty
---------- -----------
李三         143
李三         143
黄五         125
黄五         125
张三         101
张三         101

(6 行受影响)
*/

作者: pengxuan   发布时间: 2011-11-19

引用 5 楼 marklr 的回复:
to :AcHerat

from 多个表这是叫左连接还是叫什么?

from 多个表叫多表联接
多表联接又分好几种
left join 左联接
right join 右联接
full join 全联接
inner join 内联接
cross join 交叉联接

作者: pengxuan   发布时间: 2011-11-19

那像AcHerat这种 from tb a,b 而没有写left join 或者right join 就一个 where a.id=b.id 之类的叫什么连接?

,另外你写的over partition by 这个命令很好用,学习了

作者: marklr   发布时间: 2011-11-19

lz的意思很模糊啊,让人遐想菲菲,不过按照正常人的想法你是想要这样的结果吧
SQL code
name  总分
----   ---
李三    143
黄五    125
张三    101
select name,sum(score) as [总分] from t1 
group by name 
order by sum(score) desc

作者: xiaolinyouni   发布时间: 2011-11-19

引用 5 楼 marklr 的回复:
to :AcHerat

from 多个表这是叫左连接还是叫什么?

这个就是内连接。

作者: fredrickhu   发布时间: 2011-11-19

引用 9 楼 marklr 的回复:
那像AcHerat这种 from tb a,b 而没有写left join 或者right join 就一个 where a.id=b.id 之类的叫什么连接?

,另外你写的over partition by 这个命令很好用,学习了

OVER函数 可以查查联机丛书。

作者: fredrickhu   发布时间: 2011-11-19

热门下载

更多