请教sql如何按照汇总的结果来排序?
时间:2011-11-19
来源:互联网
name score
张三 23
张三 78
黄五 57
黄五 68
李三 65
李三 78
作者: marklr 发布时间: 2011-11-19
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
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
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
from 多个表这是叫左连接还是叫什么?
作者: marklr 发布时间: 2011-11-19
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
to :AcHerat
from 多个表这是叫左连接还是叫什么?
from 多个表叫多表联接
多表联接又分好几种
left join 左联接
right join 右联接
full join 全联接
inner join 内联接
cross join 交叉联接
作者: pengxuan 发布时间: 2011-11-19
,另外你写的over partition by 这个命令很好用,学习了
作者: marklr 发布时间: 2011-11-19
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
to :AcHerat
from 多个表这是叫左连接还是叫什么?
这个就是内连接。
作者: fredrickhu 发布时间: 2011-11-19
那像AcHerat这种 from tb a,b 而没有写left join 或者right join 就一个 where a.id=b.id 之类的叫什么连接?
,另外你写的over partition by 这个命令很好用,学习了
OVER函数 可以查查联机丛书。
作者: fredrickhu 发布时间: 2011-11-19
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28