1.员工表t_employee eid name ---------------- a01 张三 a02 李四 create table t_employee ( eid nvarchar(10), name nvarchar(10) ) insert into t_employee values('a01','张三') insert into t_employee values('a02','李四') 工资表t_salary sid eid type amount ----------------------------------- 101 a01 基本工资 5500 102 a01 绩效工资 2000 103 a01 奖 金 1500 104 a02 基本工资 6500 105 a02 绩效工资 2200 106 a02 奖 金 1300 create table t_salary( sid nvarchar(20), eid nvarchar(20), type nvarchar(20), amount int ) insert into t_salary values('101','a01','基本工资',5500) insert into t_salary values('102','a01','绩效工资',2000) insert into t_salary values('103','a01','奖 金',1500) insert into t_salary values('104','a02','基本工资',6500) insert into t_salary values('105','a02','绩效工资',2200) insert into t_salary values('106','a02','奖 金',1300) 我现在要的查询结果是: 姓名 基本工资 绩效工资 奖 金 合 计 --------------------------------------------------------- 张三 5500 2000 1500 9000 李四 6500 2200 1300 10000 select t_employee.name, sum(case type when '基本工资' then amount else 0 end) 基本工资, sum(case type when '绩效工资' then amount else 0 end) 绩效工资, sum(case type when '奖金' then amount else 0 end) 奖金, sum(amount) 合计 from t_employee,t_salary where t_employee.eid=t_salary.eid group by t_employee.name
如果是oracle SELECT a.NAME, sum(DECODE (b.TYPE, '基本工资', b.amount)) AS 基本工资, sum(DECODE (b.TYPE, '绩效工资', b.amount)) AS 绩效工资, sum(DECODE (b.TYPE, '奖金', b.amount)) AS 奖金,sum(b.amount) as 合计 FROM t_employee a, t_salary b WHERE a.eid = b.eid group by a.name
2.-----------------------------------------------------------------------------------------------------------------
一个表有三列,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 else c end) from t;
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 table_shengfu( rq datetime, shengfu nvarchar(10) ) insert into table_shengfu values('2005-05-09','胜') insert into table_shengfu values('2005-05-09','胜') insert into table_shengfu values('2005-05-09','负') insert into table_shengfu values('2005-05-09','负') insert into table_shengfu values('2005-05-10','胜') insert into table_shengfu values('2005-05-10','负') insert into table_shengfu values('2005-05-10','负') 答案: --解一 select rq, sum(case when shengfu='胜' then 1 else 0 end) as胜, sum(case when shengfu='负' then 1 else 0 end) as负 from table_shengfu group by rq --解二 select N.rq,N. 胜,M. 负from (select rq,count(*) 胜from table_shengfu where shengfu='胜'group by rq)N inner join (select rq,count(*) 负from table_shengfu where shengfu='负'group by rq)M on N.rq=M.rq
------------------------------------------------------------------------------------------------------------------------
3.一个日期判断的sql语句 请取出tab5表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间) --------------------------------------------------------------------------------------------- (oracle) select * from tab5 t where to_char(t.SendTime,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd') (sql server) select * from table_shengfu where CONVERT(varchar(100), rq, 23) = CONVERT(varchar(100), GETDATE(), 23) select * from table_shengfu where CONVERT(varchar(100), rq, 23) = '2005-05-10'
--------------------------------------------------------------------------------------------------------------------------------
4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 显示格式: 语文 数学 英语 及格 优秀 不及格 ------------------------------------------------------------------------------------------- select (case when语文>=80 then '优秀' when语文>60 then '及格' else '不及格' end) as 语文, (case when 数学>=80 then '优秀' when数学>60 then '及格' else '不及格' end) as数学, (case when英语>=80 then '优秀' when英语>60 then '及格' else '不及格' end) as 英语 from tab5
5.请用一个sql语句得出结果 从table1,table2中取出如table3所列格式数据 table1 月份mon 部门dep 业绩yj ------------------------------- 一月份 01 10 一月份 02 10 一月份 03 5 二月份 02 8 二月份 04 9 三月份 03 8 table2 部门dep 部门名称depname -------------------------------- 01 国内业务一部 02 国内业务二部 03 国内业务三部 04 国际业务部 table3 (result) 部门dep 一月份 二月份 三月份 --------------------------------------------------- 01 10 null null 02 10 8 null 03 5 null 8 04 null 9 null ------------------------------------------------------------------------------------------- 1) select t.depname, (select yj from tab6 where mon='一月份' and dep=t.dep) 一月份, (select yj from tab6 where mon='二月份' and dep=t.dep) 二月份, (select yj from tab6 where mon='三月份' and dep=t.dep) 三月份 from tab7 t --------------------------------------------------------- 2)求总销售额 select sum(case when t1.mon='一月份' then t1.yj else 0 end) 一月份, sum(case when t1.mon='二月份' then t1.yj else 0 end) 二月份, sum(case when t1.mon='三月份' then t1.yj else 0 end) 三月份 from tab7 t,tab6 t1 where t.dep=t1.dep
6.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。 ------------------------------------------------------------------------------- select id,count(*) from tab8 group by id having count(*)>1 select * from (select tab8,count(id) as num from tab8 group by id) t where t.num>1
7.用一条SQL语句查询出每门课都大于分的学生姓名 name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90 select distinct name from tab9 where name not in (select distinct name from tab9 where fengshu<=80) select * from tab9 t7 where t7.name not in (select t5.name from (select * from (select t1.kecheng from tab9 t1 group by t1.kecheng), (select t2.name from tab9 t2 group by t2.name)) t4, (select * from tab9) t5 where t4.name = t5.name and t4.kecheng = t5.kecheng and t5.fengshu < 80 )