SQL查询总结 - wanglei

SQL查询总结(2010-03-08 16:39:07) 转载▼ 标签: 杂谈 分类: system 1、SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 2005-05-10 负 如果要生成下列效果, 该如何写sql语句? 胜 负 2005- http://www.star1111.info 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   2.请教一个面试中遇到的SQL语句的查询疑问 表中有A B C三列,用SQL语句完结:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。 ------------------------------------------ select (case when a>b then a else b end ), (case when b>c then b esle c end) from table_name   3.面试题:一个日期区分的sql语句? 请取出tb_send表中日期(SendTime字段)为当天的全部记载?(SendTime字段为datetime型,包含日期与时辰) ------------------------------------------ select * from tb where datediff(dd,SendTime,getdate())=0 4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记载分别标明语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记载并按以下条件闪现出来(并写出您的思路): 大于或等于80标明优良,大于或等于60标明及格,小于60分标明不及格。 闪现格式: 语文 数学 英语 及格 优良 不及格 ------------------------------------------ select (case when 语文>=80 then '优良' when 语文>=60 then '及格' else '不及格') as 语文, (case when 数学>=80 then '优良' when 数学>=60 then '及格' else '不及格') as 数学, (case when 英语>=80 then '优良' when 英语>=60 then '及格' else '不及格') as 英语, from table   5.在sqlserver2000中请用sql创建一张用户暂时表和系统暂时表,里面包含两个字段ID和IDValues,类型都是int型,并解说下两者的区别? ------------------------------------------ 用户暂时表:create table #xx(ID int, IDValues int) 系统暂时表:create table ##xx(ID int, IDValues int) 区别: 用户暂时表只对创建这个表的用户的Session可见,对其他进程是不可见的. 当创建它的进程不见时这个暂时表就自动删去. 全局暂时表对整个SQL Server实例都可见,可是全部访问它的Session都不见的时分,它也自动删去.   6.sqlserver2000是一种大型数据库,他的存储容量只受存储介质的束缚,请问它是通过啥办法完结这种无限容量机制的。 ------------------------------------------ 它的全部数据都存储在数据文件中(*.dbf),所以只需文件够大,SQL Server的存储容量是可以扩大的. SQL Server 2000 数据库有三种类型的文件: 首要数据文件 首要数据文件是数据库的起点,指向数据库中文件的其它有些。每个数据库都有一个首要数据文件。首要数据文件的举荐文件扩大名是 .mdf。 非有必要数据文件 非有必要数据文件包含除首要数据文件外的全部数据文件。有些数据库可以没有非有必要数据文件,而有些数据库则有多个非有必要数据文件。非有必要数据文件的举荐文件扩大名是 .ndf。 日志文件 日志文件包含恢复数据库所需的全部日志信息。每个数据库有必要至少有一个日志文件,但可以不止一个。日志文件的举荐文件扩大名是 .ldf。   7.请用一个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 ------------------------------------------ 1) select a.有些称谓dname,b.成果yj as '一月份',c.成果yj as '二月份',d.成果yj as '三月份' from table1 a,table2 b,table2 c,table2 d where a.有些dep = b.有些dep and b.月份mon = '一月份' and a.有些dep = c.有些dep and c.月份mon = '二月份' and a.有些dep = d.有些dep and d.月份mon = '三月份' and 2) select a.dep, sum(case when b.mon=1 then b.yj else 0 end) as '一月份', sum(case when b.mon=2 then b.yj else 0 end) as '二月份', sum(case when b.mon=3 then b.yj else 0 end) as '三月份', sum(case when b.mon=4 then b.yj else 0 end) as '四月份', sum(case when b.mon=5 then b.yj else 0 end) as '五月份', sum(case when b.mon=6 then b.yj else 0 end) as '六月份', sum(case when b.mon=7 then b.yj else 0 end) as '七月份', sum(case when b.mon=8 then b.yj else 0 end) as '八月份', sum(case when b.mon=9 then b.yj else 0 end) as '九月份', sum(case when b.mon=10 then b.yj else 0 end) as '十月份', sum(case when b.mon=11 then b.yj else 0 end) as '十一月份', sum(case when b.mon=12 then b.yj else 0 end) as '十二月份', from table2 a left join table1 b on a.dep=b.dep   8.华为一道面试题 一个表中的Id有多个记载,把全部这个id的记载查出来,并闪现共有多少条记载数。 ------------------------------------------ select id, Count(*) from tb group by id having count(*)>1 select * from(select count(ID) as count from table group by ID)T where T.count>1 9, 查询出出售表中,出售额大于本地区平均水平的记载,用一条sql语句就搞定了。 Sales表                                                                                       OrderID                          Region                          Total                          1                          A                          100.00                          2                          C                          80.00                          3                          A                          130.00                          4                          B                          90.00                          5                          B                          100.00                          6                          C                          120.00                          7                          A   Sql语句: select * from sales as s inner join (select avg(total) as avge,region fromsales group by region) avgtable on s.region = avgtable.region where total > avgtable.avge http://www.6699ysk.info 
posted @ 2013-03-29 05:35  chinadiy197601  阅读(219)  评论(0编辑  收藏  举报