SQL查询面试题
2012-11-09 22:43
357 查看
1、统计每年每月的信息
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
准备sql语句:
drop table if exists sales;
create table sales(id int auto_increment primary key,year varchar(10), month varchar(10), amount float(2,1));
insert into sales values
(null,’1991′,’1′,1.1),
(null,’1991′,’2′,1.2),
(null,’1991′,’3′,1.3),
(null,’1991′,’4′,1.4),
(null,’1992′,’1′,2.1),
(null,’1992′,’2′,2.2),
(null,’1992′,’3′,2.3),
(null,’1992′,’4′,2.4);
答案:
select sales.year ,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’1′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’2′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’3′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) as ’4′
from sales group by year;
2、分组统计胜负
表内容:
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','负')
1)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
2) select N.rq,N.勝,M.負 from (
select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join
(select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq
3)select a.col001,a.a1 胜,b.b1 负 from
(select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a,
(select col001,count(col001) b1 from temp1 where col002='负' group by col001) b
where a.col001=b.col001
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
准备sql语句:
drop table if exists sales;
create table sales(id int auto_increment primary key,year varchar(10), month varchar(10), amount float(2,1));
insert into sales values
(null,’1991′,’1′,1.1),
(null,’1991′,’2′,1.2),
(null,’1991′,’3′,1.3),
(null,’1991′,’4′,1.4),
(null,’1992′,’1′,2.1),
(null,’1992′,’2′,2.2),
(null,’1992′,’3′,2.3),
(null,’1992′,’4′,2.4);
答案:
select sales.year ,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’1′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’2′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) ’3′,
(select t.amount from sales t where t.month=’1′ and t.year= sales.year) as ’4′
from sales group by year;
2、分组统计胜负
表内容:
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','负')
1)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
2) select N.rq,N.勝,M.負 from (
select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join
(select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq
3)select a.col001,a.a1 胜,b.b1 负 from
(select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a,
(select col001,count(col001) b1 from temp1 where col002='负' group by col001) b
where a.col001=b.col001
相关文章推荐
- SQL面试题之一有关连接、查询的题
- SQL面试题 三(单表、多表查询)
- 面试题:查询所有上级SQL
- 一道简单的SQL面试题:查询成绩排名第10到第20的学生
- T-SQL 面试题(查询)
- sql 查询面试题
- SQL 面试题 二 (有关排序、模糊查询)
- 面试题:用一条sql语句查询出“每门”课程都大于80分的学生姓名
- sql查询之左连接,右连接,内连接以及全外连接的使用(测试常见面试题欧)
- SQL Server ->> T-SQL查询面试题之实例版
- 一道sql面试题(查询语句)
- SQL 子查询 面试题
- 面试题中的sql语句经典查询
- sql语句面试题(查询按照申请日期倒序排列的前10个记录)
- sql 相关的查询面试题收集及答案
- sql 多表查询 统计无记录缺考--- 经典面试题
- 由一个博问学到的SQL查询方法 (一道多对多关系查询的面试题)
- SQL查询面试题
- 面试题:用一条sql语句查询出“每门”课程都大于80分的学生姓名
- sql语句面试题(查询按照申请日期倒序排列的前10个记录)