【问】:查询语句中同时使用sum,max 怎么处理?

qq120848369 2010-05-09 12:09:44
我想实现对每个group先用sum求和,然后取和最大的group与其sum值。

大体意思这样:

select name,sum(score)
from student
group by name
having max(sum(score))

意思是这样,但是肯定不是这样写,高手能指导一下么.万分感谢
...全文
973 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
----------------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-05-09 12:49:09
-- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
----------------------------------------------------------------------------------

--> 生成测试数据表: [student]
IF OBJECT_ID('[student]') IS NOT NULL
DROP TABLE [student]
GO
CREATE TABLE [student] ([name] [nvarchar](10),[score] [int])
INSERT INTO [student]
SELECT 'a','20' UNION ALL
SELECT 'b','40' UNION ALL
SELECT 'a','40' UNION ALL
SELECT 'b','60' UNION ALL
SELECT 'c','59' UNION ALL
SELECT 'c','64' UNION ALL
SELECT 'd','89' UNION ALL
SELECT 'd','93' UNION ALL
SELECT 'e','98' UNION ALL
SELECT 'e','99' UNION ALL
SELECT 'f','100' UNION ALL
SELECT 'f','80'

--SELECT * FROM [student]

-->SQL查询如下:
select top 1 name,sum(score) maxscore
from student
group by name
order by sum(score) desc
/*

name maxscore
---------- -----------
e 197

(1 行受影响)
*/
喜-喜 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qq120848369 的回复:]
引用楼主 qq120848369 的回复:
我想实现对每个group先用sum求和,然后取和最大的group与其sum值。

大体意思这样:

select name,sum(score)
from student
group by name
having max(sum(score))

意思是这样,但是肯定不是这样写,高手能指导一下么.万分感谢


select na……
[/Quote]

聚合函数里面不能出现聚合函数的...
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
so sorry to htl258 ,i forgot the "top 1" .. thanks a lot to you all.
喜-喜 2010-05-09
  • 打赏
  • 举报
回复
select top 1 name,score
from (select name,score=sum(score)
from student group by name)t
order by score desc
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qq120848369 的回复:]
照着2#的写了一下,结果每个人的都打印出来了,我只想打印sum()最大的那个,该怎么处理呢。
[/Quote]怎么可能,没有照好吧
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 qq120848369 的回复:]
我想实现对每个group先用sum求和,然后取和最大的group与其sum值。

大体意思这样:

select name,sum(score)
from student
group by name
having max(sum(score))

意思是这样,但是肯定不是这样写,高手能指导一下么.万分感谢
[/Quote]

select name,max(sum(score)) //这里我描述错了,这样该怎么写
from student
group by name
having max(sum(score))
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
照着2#的写了一下,结果每个人的都打印出来了,我只想打印sum()最大的那个,该怎么处理呢。
sql_lover 2010-05-09
  • 打赏
  • 举报
回复

WITH T AS
(select name,sum(score) AS total
from student
group by NAME)
SELECT * FROM T b
WHERE NOT EXISTS (SELECT 1 FROM T WHERE total>b.total)
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
select top 1 name,sum(score) maxscore
from student
group by name
order by sum(score) desc
rmljoe 2010-05-09
  • 打赏
  • 举报
回复
select top 1
name,
scoreT
from
(select name,sum(score) as scoreT
from student
group by name order by scoreT desc)

jbz001 2010-05-09
  • 打赏
  • 举报
回复
我只路过
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
只有这样了额.
没学过的确不好写,感谢大家乐.
喜-喜 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 qq120848369 的回复:]
引用 20 楼 htl258 的回复:

SQL code
select a.项目编号, b.项目名称,c.运动员姓名,sum(a.积分) 总积分
from 成绩 a
join 项目 b on a.项目编号=b.项目编号
join 运动会 c on a.运动员编号=c.运动员编号
group by a.项目编号,b.项目名称,c.运动员姓名
having sum(a.积分)=(se……
[/Quote]

有些内容不是课堂上能学的到的...
feixianxxx 2010-05-09
  • 打赏
  • 举报
回复

提完一个又一个

作业题还是自己做比较好 。。。
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 htl258 的回复:]

SQL code
select a.项目编号, b.项目名称,c.运动员姓名,sum(a.积分) 总积分
from 成绩 a
join 项目 b on a.项目编号=b.项目编号
join 运动会 c on a.运动员编号=c.运动员编号
group by a.项目编号,b.项目名称,c.运动员姓名
having sum(a.积分)=(select top 1 sum(积分) from……
[/Quote]

怎么感觉不是题目的意思呢...join语句什么作用, 我就学了数据库概论,SQL语句好多不懂.
老师布置这个作业难免太超前了
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
select a.项目编号, b.项目名称,c.运动员姓名,sum(a.积分) 总积分
from 成绩 a
join 项目 b on a.项目编号=b.项目编号
join 运动会 c on a.运动员编号=c.运动员编号
group by a.项目编号,b.项目名称,c.运动员姓名
having sum(a.积分)=(select top 1 sum(积分) from 成绩 where 项目编号=a.项目编号 order by sum(积分) desc)
换一下
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
select b.项目名称,c.运动员姓名
from 成绩 a
join 项目 b on a.项目编号=b.项目编号
join 运动会 c on a.运动员编号=c.运动员编号
group by b.项目名称,c.运动员姓名
having sum(a.积分)=(select top 1 sum(积分) from 成绩 where 项目编号=a.项目编号 order by sum(积分) desc)
try
hoojo 2010-05-09
  • 打赏
  • 举报
回复
select top 1 name,sum(score) maxscore
from student
group by name
order by sum(score) desc
qq120848369 2010-05-09
  • 打赏
  • 举报
回复
额。我也理解不了,我把题目描述出来。

运动会(运动员编号,运动员姓名,运动员性别,所属系名)
项目(项目编号,项目名称,项目比赛地点)
成绩(运动员编号,项目编号,积分)

题目: 找出在1号操场进行比赛的各项目名称与其冠军的姓名.

大大们写一下这个问题,我学习一下写法,谢谢.
htl258_Tony 2010-05-09
  • 打赏
  • 举报
回复
还是要这个:

select name,score
from student t
where score=(select max(score) from student where name=t.name)
加载更多回复(5)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧