SQL Server查询符合条件的某个字段的总和及明细

贝爷_野外求生 2017-02-21 03:09:38
假如有如下表table1
--------------------
name age sex course score
张三 17 男 数学 88
李四 16 女 语文 78
张三 17 男 英语 58
王二 18 男 语文 92
李四 16 女 数学 80
--------------------
我要查询course='数学' and age=17的分数总和,并且显示每一条的详细
这个怎么在一句里执行出来?
试过group by只能根据一个字段分组,我这里是根据查询的结果作为一组。
如何查询其结果的score总和及其每一条的明细?
虚心请教
...全文
1706 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 版主 2017-02-21
  • 打赏
  • 举报
回复
引用 5 楼 dr592112441 的回复:
[quote=引用 3 楼 sinat_28984567 的回复:] [quote=引用 2 楼 u011981242 的回复:] [quote=引用 1 楼 sinat_28984567 的回复:] 最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146
是这样的,就是将下面的2句合成1句: select * from table1 where course='数学' and age =17 select sum(score) from table1 where course='数学' and age =17[/quote] select *,(select sum(score) from table1 where course='数学' and age =17)AS all from table1 where course='数学' and age =17[/quote] 你确定是对的?[/quote] 按照楼主的要求应该是对的
Mirror然 2017-02-21
  • 打赏
  • 举报
回复
引用 3 楼 sinat_28984567 的回复:
[quote=引用 2 楼 u011981242 的回复:] [quote=引用 1 楼 sinat_28984567 的回复:] 最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146
是这样的,就是将下面的2句合成1句: select * from table1 where course='数学' and age =17 select sum(score) from table1 where course='数学' and age =17[/quote] select *,(select sum(score) from table1 where course='数学' and age =17)AS all from table1 where course='数学' and age =17[/quote] 你确定是对的?
  • 打赏
  • 举报
回复
引用 3 楼 sinat_28984567 的回复:
[quote=引用 2 楼 u011981242 的回复:] [quote=引用 1 楼 sinat_28984567 的回复:] 最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146
是这样的,就是将下面的2句合成1句: select * from table1 where course='数学' and age =17 select sum(score) from table1 where course='数学' and age =17[/quote] select *,(select sum(score) from table1 where course='数学' and age =17)AS all from table1 where course='数学' and age =17[/quote]很不错,成功了。感谢感谢。
二月十六 版主 2017-02-21
  • 打赏
  • 举报
回复
引用 2 楼 u011981242 的回复:
[quote=引用 1 楼 sinat_28984567 的回复:] 最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146
是这样的,就是将下面的2句合成1句: select * from table1 where course='数学' and age =17 select sum(score) from table1 where course='数学' and age =17[/quote] select *,(select sum(score) from table1 where course='数学' and age =17)AS all from table1 where course='数学' and age =17
  • 打赏
  • 举报
回复
引用 1 楼 sinat_28984567 的回复:
最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146
是这样的,就是将下面的2句合成1句: select * from table1 where course='数学' and age =17 select sum(score) from table1 where course='数学' and age =17
二月十六 版主 2017-02-21
  • 打赏
  • 举报
回复
最后想得到什么样一个结果,是下边这样的么? name age sex course score all 张三 17 男 数学 88 146

34,838

社区成员

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

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