数据库的一些例子及其分析
2009-09-08 10:04
253 查看
1、表
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
胜 负
2005-05-09 2 2
2005-05-10 1 2
create table #tmp(rq varchar(10),shengfu nchar(1))
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-10','胜')
insert into #tmp values('2005-05-10','负')
insert into #tmp values('2005-05-10','负')
分析:其实可以先写这样的sql语句:
select rq, ( case when shengfu = '胜' then 1 else 0 end)胜,
( case when shengfu ='负' then 1 else 0 end)负
from temp
产生如下的结果:
rq 胜 负
2005-05-09 1 0
2005-05-09 1 0
2005-05-09 0 1
2005-05-09 0 1
2005-05-10 1 0
2005-05-10 0 1
2005-05-10 0 1
然后就group一下就行了!
比较好的写法是:
select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq
另一种思路:
select a.rq,a.胜,b.负 from
(select rq, count(sf) 胜 from temp where sf='胜' group by rq )a
inner join
(select rq, count (sf) 负 from temp where sf='负' group by rq) b
on a.rq=b.rq
2、请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。
table1
月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8
table2
部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部
table3 (result)
部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9
------------------------------------------
思路:这个题实际上只需用到table1 而且table3的答案有点问题
select dep,sum(case when mon='一月份' then yj else 0 end)一月份,
sum(case when mon='二月份' then yj else 0 end)二月份,
sum(case when mon='三月份' then yj else 0 end)三月份
from table1 group by dep order by dep)a ;
结果是:
部门dep 一月份 二月份 三月份
--------------------------------------
01 10 0 0
02 10 8 0
03 5 8 0
04 0 9 0
------------------------------------------
然后就把0转为null就行了,其实一开始还是用0来算 这样group就方便了
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
胜 负
2005-05-09 2 2
2005-05-10 1 2
create table #tmp(rq varchar(10),shengfu nchar(1))
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-10','胜')
insert into #tmp values('2005-05-10','负')
insert into #tmp values('2005-05-10','负')
分析:其实可以先写这样的sql语句:
select rq, ( case when shengfu = '胜' then 1 else 0 end)胜,
( case when shengfu ='负' then 1 else 0 end)负
from temp
产生如下的结果:
rq 胜 负
2005-05-09 1 0
2005-05-09 1 0
2005-05-09 0 1
2005-05-09 0 1
2005-05-10 1 0
2005-05-10 0 1
2005-05-10 0 1
然后就group一下就行了!
比较好的写法是:
select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq
另一种思路:
select a.rq,a.胜,b.负 from
(select rq, count(sf) 胜 from temp where sf='胜' group by rq )a
inner join
(select rq, count (sf) 负 from temp where sf='负' group by rq) b
on a.rq=b.rq
2、请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。
table1
月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8
table2
部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部
table3 (result)
部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9
------------------------------------------
思路:这个题实际上只需用到table1 而且table3的答案有点问题
select dep,sum(case when mon='一月份' then yj else 0 end)一月份,
sum(case when mon='二月份' then yj else 0 end)二月份,
sum(case when mon='三月份' then yj else 0 end)三月份
from table1 group by dep order by dep)a ;
结果是:
部门dep 一月份 二月份 三月份
--------------------------------------
01 10 0 0
02 10 8 0
03 5 8 0
04 0 9 0
------------------------------------------
然后就把0转为null就行了,其实一开始还是用0来算 这样group就方便了
相关文章推荐
- IDEF1x语义建模方法及其在数据库设计中的应用3----例子:选课系统
- PostgreSQL数据库内核分析 笔记(这本书没有怎么很好的看,主要就是一些数据结构、概念和流程的文字介绍)
- 一些DOT.NET的数据库(SQL)操作例子
- 一些影响数据库访问速度的原因分析
- 两主机互ping的一些例子及其解释
- 一些运行时异常及其对应的例子
- 时间序列数据库——索引用ES、聚合分析时加载数据用什么?docvalues的列存储貌似更优优势一些
- 数据库的一些常见面试题及其答案详解
- 初始化数据库数据的一些例子
- android音乐播放器例子及其源码分析
- 分析数据库的一些方法
- Hive SQL 一些窗口函数,分析函数的使用小例子
- Android数据库一些源码分析
- 数据库性能优化之索引设计原则及其分析
- 一些ADO数据库入门例子与资料
- 第九节:从源码的角度分析MVC中的一些特性及其用法
- 两主机互ping的一些例子及其解释
- 做的一些测试数据的分析 一个是直接循环插入数据库 一个是循环好了放在数组里 在插入数据库
- 两主机互ping的一些例子及其解释
- 数据库设计中的一些图及其注意事项