求一个省份统计sql, group by 相关

wdwwtzy 2012-02-02 03:38:31
有一省份表,现想按省份统计出每个省份有多少个学生,多少个老师,总分是多少
弄了半天没弄出来,求救一下各位

测试数据其期望结果见下

create table province
(
id int,
name nvarchar(50)
)
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'

create table student
(
id int,
name nvarchar(50),
provinceId int
)
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2

create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3

create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200

--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0


...全文
262 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
victor_yang 2012-02-02
  • 打赏
  • 举报
回复
我已经写了个子查询的给你了,看看吧
勿勿 2012-02-02
  • 打赏
  • 举报
回复
create table province
(
id int,
name nvarchar(50)
)


insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'

create table student
(
id int,
name nvarchar(50),
provinceId int
)

insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2


create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3

create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200

--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0


select a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id


id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0
警告: 聚合或其他 SET 操作消除了 Null 值。

(3 行受影响)



wdwwtzy 2012-02-02
  • 打赏
  • 举报
回复
另外,我在想可不可以用子查询的方式来做呢?谁教教我...连接查询有各种情况出现...
victor_yang 2012-02-02
  • 打赏
  • 举报
回复
select a.*,student=isnull(student,0),teacher=isnull(teacher,0),totalscore=isnull(totalScore,0)
from province a
left join (select provinceid,student=count(id) from student group by provinceid) b
on a.id = b.provinceid
left join (select provinceid,teacher=count(id) from teacher group by provinceid) c
on a.id = c.provinceid
left join (select s2.provinceid,totalScore=sum(s1.score) from score s1 left join student s2 on s1.studentId=s2.id group by s2.provinceid) d
on a.id = d.provinceid

id name student teacher totalscore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0

(所影响的行数为 3 行)
wdwwtzy 2012-02-02
  • 打赏
  • 举报
回复
呃,多谢,原来可以count里加distinct啊....郁闷....
  • 打赏
  • 举报
回复

create table province
(
id int,
name nvarchar(50)
)
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'

create table student
(
id int,
name nvarchar(50),
provinceId int
)
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2

create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3

create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200

--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0

select province.id,province.name,
count(distinct student.name) as student,
count(distinct teacher.name) as teacher,
isnull(sum(score),0) as totalscore
from province
left join student on province.id=student.provinceId
left join teacher on province.id=teacher.provinceId
left join score on student.id=score.studentId
group by province.id,province.name
order by province.id asc

/*
--结果:
id name student teacher totalscore
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0L

*/

Rotel-刘志东 2012-02-02
  • 打赏
  • 举报
回复
---测试数据 创建表province
create table province
(
id int,
name nvarchar(50)
)
---向province插入记录
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'
----创建表student
create table student
(
id int,
name nvarchar(50),
provinceId int
)
---student表插入记录
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2

create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3

create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200


----查询
SQL codeselect a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id
/*
id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0

(3 行受影响)
*/
百年树人 2012-02-02
  • 打赏
  • 举报
回复
select a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id
/**
id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0

(3 行受影响)
**/
wdwwtzy 2012-02-02
  • 打赏
  • 举报
回复
呃,楼上的结果不对啊...
  • 打赏
  • 举报
回复

create table province
(
id int,
name nvarchar(50)
)
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'

create table student
(
id int,
name nvarchar(50),
provinceId int
)
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2

create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3

create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200

--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0

select province.id,province.name,
count(student.name) as student,
COUNT(teacher.name) as teacher,
sum(score)totalscore
from province
left join student on province.id=student.provinceId
left join teacher on province.id=teacher.provinceId
left join score on student.id=score.studentId
group by province.id,province.name
order by province.id asc

/*
--结果:
id name student teacher totalscore
1 北京 2 2 130
2 上海 3 3 200
3 广东 0 2 NULL

*/
Rotel-刘志东 2012-02-02
  • 打赏
  • 举报
回复
搞清楚表间的关系,连接查询就可以实现的。
手把手视频详细讲解项目开发全过程,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。 课程简介 从0开始构建BI商业大数据分析平台,以实际的电商分析业务,贯穿数据生成,数据仓库、ETL、数据分析以及可视化分析和商业BI报表,让你三天零基础快速搭建商业化BI分析平台 课程亮点 1,零基础讲解,从数据存储到商业可视化分析,一步步逐渐深入 2,理论+实践,让你既能学懂也能学会 3,图文并茂,化繁为简,让知识通俗易懂,不再抽象 4,以实际企业业务需讲解,让学生学有所用,学完即可上手BI 适用人群 1、对大数据感兴趣的在校生及应届毕业生。 2、对目前职业有进一步提升要,希望从事大数据行业高薪工作的在职人员。 3、对大数据行业感兴趣的相关人员。 课程内容 第一章:MySQL数据库入门 1.1 大数据应用及数据存储 1.2 MySQL的介绍 1.3 MySQL数据库部署 1.4 DataGrip可视化工具部署 1.5 DataGrip与MySQL连接配置 第二章:SQL分析之DDL 2.1 SQL的分类与规则 2.2 DDL之数据库管理 2.3 DDL之数据表创建与查询 2.4 DDL之数据表删除与描述 第三章:SQL分析之DML 3.1 DML之数据的插入 3.2 DML之数据的修改 3.3 DML之数据的删除 第四章:SQL分析之DQL 4.1 DQL基本查询规则 4.2 DQL之入门查询 4.3 DQL之条件查询where 4.4 DQL之聚合查询聚合函数 4.5 DQL之分组查询group by 4.6 DQL之排序查询order by 4.7 DQL之分页查询limit 4.8 DQL之结果保存 第五章:多表复杂分析查询 5.1 多表查询:表与表之间的关系 5.2 多表查询:Join关联 5.3 多表查询:子查询 第六章:数据仓库与ETL 6.1 数据仓库入门 6.2 ETL与Kettle的介绍 6.3 JDK及Kettle的部署 第七章:Kettle的ETL开发实战 7.1 Kettle中概念详解 7.2 Kettle实现文本输入与Excel输出 7.3 Kettle实现Excel输入与表输出 7.4 表输入组件开发 7.5 插入更新组件开发 7.6 Kettle作业开发 第八章:BI可视化与实战需 8.1 数据可视化背景 8.2 FIneBI的功能与部署 8.3 电商业务介绍 8.4 技术架构及数据准备 第九章:ETL与数据分析实战 9.1 基于MySQL实现数据内容拆解 9.2 基于Kettle实现数据的ETL 9.3 指标分析:每日订单总额及订单个数 9.4 指标分析:独立用户数以及热门商品Top10 9.5 指标分析:每天每个小时上架商品及订单个数 9.6 指标分析:每日不同支付方式的订单总金额及订单个数 9.7 指标分析:每日每个省份的订单总金额及订单个数 9.8 指标分析:每日不同商品分类的订单个数统计 第十章:BI可视化分析实战 10.1 FIneBI的基本使用 10.2 仪表盘显示每日订单信息 10.3 柱状图及曲线图的构建 10.4 饼图及雷达图的构建 10.5 混合图及词云图构建

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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