2015-09-10 06:30:02

阅读( 187 )

1、关于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-05-09 2 2

2005-05-10 1 2

答案:

1) select rq,sum(case when shengfu=’胜’ then 1 else 0 end) as胜,sum(case whenshengfu=’负’ then 1 else 0 end) as负from tab3 group by rq

2) select N.rq,N. 胜,M. 负 from

(select rq,count(*) 胜 from tab3 where shengfu=’胜’group by rq)N inner join

(select rq,count(*) 负from tab3 where shengfu=’负’group by rq)M on N.rq=M.rq

3) select a.rq,a. 胜 as胜,b.负 as 负from

(select rq,count(shengfu) 胜from tab3 where shengfu=’胜’ group by rq) a,

(select rq,count(shengfu) 负from tab3 where shengfu=’负’ group by rq) b

where a.rq=b.rq;

1.关于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-05-09 2 2

2005-05-10 1 2

——————————————————————————————–

1) select rq,sum(case when shengfu=’胜’ then 1 else 0 end) as胜,sum(case whenshengfu=’负’ then 1 else 0 end) as负from tab3 group by rq

2) select N.rq,N. 胜,M. 负 from

(select rq,count(*) 胜 from tab3 where shengfu=’胜’group by rq)N inner join

(select rq,count(*) 负from tab3 where shengfu=’负’group by rq)M on N.rq=M.rq

3) select a.rq,a. 胜 as胜,b.负 as 负from

(select rq,count(shengfu) 胜from tab3 where shengfu=’胜’ group by rq) a,

(select rq,count(shengfu) 负from tab3 where shengfu=’负’ group by rq) b

where a.rq=b.rq;

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) fromtab4

3.一个日期判断的sql语句

请取出tab5表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)

———————————————————————————————

select * from tab5 t where to_char(t.SendTime,’yyyy-mm-dd’)=to_char(sysdate,’yyyy-mm-dd’)

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语句 查询出每门课都大于80分的学生姓名

name kecheng fenshu

张三 语文 81

张三 数学 75

李四 语文 76

李四 数学 90

王五 语文 81

王五 数学 100

王五 英语 90

a): select distinct name from tab9 where name not in (select distinct name fromtab9 where fengshu<=80)

b): 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 t2group by t2.name)) t4,(select * from tab9) t5 where t4.name = t5.name and t4.kecheng = t5.kecheng and t5.fengshu

8.一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.

select t.bh||’vs’||t1.bh from tab10 t,tab10 t1 where t.bht1.bh这个是分主客场的

select t.bh||’vs’||t1.bh from tab10 t,tab10 t1 where t.bht1.bh and t.bh>t1.bh这个是不分的

9.怎么把这样一个表儿

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

a):

select t.year,

(select a.amout from tab11 a where a.month=1 and a.year=t.year) m1,

(select b.amout from tab11 b where b.month=2 and b.year=t.year) m2,

(select c.amout from tab11 c where c.month=3 and c.year=t.year) m3,

(select d.amout from tab11 d where d.month=4 and d.year=t.year) m4

from tab11 t group by t.year

10.拷贝表(拷贝数据,源表名:a 目标表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;

create table test as select * from dept; –从已知表复制数据和结构

create table test as select * from dept where 1=2; –从已知表复制结构但不包括数据

11.显示文章、提交人和最后回复时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

12.日程安排提前五分钟提醒

13.两张关联表,删除主表中已经在副表中没有的信息

delete from fubiao a where a.fid not in(select id from zhubiao)

14.有两个表tab12和tab13,均有key和value两个字段,如果tab13的key在tab12中也有,就把tab13的value换为tab12中对应的value

update tab13 set value=(select value from tab12 where tab12.key=tab13.key)

15.原表:

courseid coursename score

————————————-

1 java 70

2 oracle 90

3 xml 40

4 jsp 30

5 servlet 80

————————————-

为了便于阅读,查询此表后的结果显式如下(及格分数为60):

courseid coursename score mark

—————————————————

1 java 70 pass

2 oracle 90 pass

3 xml 40 fail

4 jsp 30 fail

5 servlet 80 pass

—————————————————

select t.courseid,t.coursename,t.score,(case when score>60 then ’pass’ else ’fail’end) mark from tab14 t

16.表15

a1 a2

1 a

1 b

2 x

2 y

2 z

用select能选成以下结果吗?

1 ab

2 xyz

17.题为

有两个表, t1, t2,

Table t1:

SELLER | NON_SELLER

—– —–

A B

A C

A D

B A

B C

B D

C A

C B

C D

D A

D B

D C

Table t2:

SELLER | BAL

—— ——–

A 100

B 200

C 300

D 400

要求用SELECT 语句列出如下结果:——如A的SUM(BAL)为B,C,D的和,B的SUM(BAL)为A,C,D的和…….

且用的方法不要增加数据库负担,如用临时表等

分享给朋友:

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息:

面试题

oracle面试 sql语句,oracle面试sql相关推荐

  1. 在Oracle中不通过存储过程一次执行多条SQL语句Oracle PL/SQL

    PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL ...

  2. oracle+执行变量语句,ORACLE sql 语句的执行过程(SQL性能调整)

    第1章 SQL语句处理的过程 在调整之前我们需要了解一些背景知识,只有知道这些背景知识,我们才能更好的去调整sql语句. 本节介绍了SQL语句处理的基本过程,主要包括: · 查询语句处理 · DML语 ...

  3. 嵌入式sql语句oracle,第十讲 嵌入式SQL语言之动态SQL

    动态SQL的概念 静态SQL特点:SQL语句在程序中已经按要求写好,只需要把一些参数通过变量(高级语言程序语句中不带冒号) 传送给嵌入式SQL语句即可(嵌入式SQL语句中带冒号) 例如: SpecNa ...

  4. oracle分页查询sql语句通用,oracle分页查询sql语句,oracle分页查询sql语句详解

    oracle分页查询sql语句,oracle分页查询sql语句详解,Oracle分页查询sql语句 Oracle中分页和MySql中的分页不同,MySql中的分页使用关键字limit即可,相对简单一点 ...

  5. oracle 执行带参数的sql语句_Oracle动态SQL语句的简单执行

    在使用ODP.NET进行Oracle编程时,有时候SQL语句非常复杂,需要采用动态构造查询语句的情况,有两种方法可以构造动态的SQL语句,并执行返回结果集. 1.在数据访问层构造SQL语句 例如下面的 ...

  6. mysql高效sql语句_高效SQL优化 非常好用的SQL语句优化34条

    高效SQL优化 非常好用的SQL语句优化34条 相关软件相关文章发表评论 来源:2011/2/13 9:38:43字体大小: 作者:佚名点击:576次评论:0次标签: 类型:电子教程大小:8.5M语言 ...

  7. oracle的分支语句,Oracle中的分支语句

    Oracle中的分支语句,Oracle条件分支语句:pl/sql中提供了三种分支语句:if-- then, if---then--else,if--then--ifesle,Oracle条 Oracl ...

  8. Java中如何解析SQL语句、格式化SQL语句、生成SQL语句?

    昨天在群里看到有小伙伴问,Java里如何解析SQL语句然后格式化SQL,是否有现成类库可以使用? 之前TJ没有做过这类需求,所以去研究了一下,并找到了一个不过的解决方案,今天推荐给大家,如果您正要做类 ...

  9. sql语句优化之SQL Server

    MS   SQL   Server查询优化方法 查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)          2.I/O吞吐量小,形成 ...

  10. tp5获取sql_tp5 sql语句 tp5 获取sql语句

    tp5 sql语句 tp5 获取sql语句以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! sql语句转换成tp5执行,求 ...

最新文章

  1. 06-hibernate注解-一对多单向外键关联
  2. C语言经典例82-八进制转换为十进制
  3. 打开和关闭mysql服务器_启动和关闭MySQL服务器
  4. 从ODA看一体机的木桶理论
  5. Android 尺寸 神图
  6. 请求转发与重定向的区别和执行流程
  7. Linux系统中,各种小动物
  8. 李永乐线性代数辅导讲义第四章学霸小结
  9. 计算机计费管理系统,中央空调时间型计费管理系统-中央空调计费系统
  10. Apache doris Datax DorisWriter扩展使用方法
  11. 反射之前奏Oracle升级版
  12. 筛选过的 Code rule
  13. springboot毕设项目商城积分兑换系统pez18(java+VUE+Mybatis+Maven+Mysql)
  14. matlab设计高通系统,用matlab设计高通滤波器雪比切夫、fir两种方法 课程设计HPF.doc...
  15. python公立,农历转换
  16. 验证IBIS模型正确并转换为DML模型图文演示
  17. 支持ESMTP身份验证的邮件发送
  18. tab s2 android 8,mini 3一边去!三星GalaxyTab S2 8.0评测
  19. Android power_supply驱动开发详解
  20. 计算机操作系统——分时系统和实时系统

热门文章

  1. 手把手教学,Python 游戏编程之实现飞机大战(含源代码)
  2. 大学英语句子课文翻译clamber
  3. unity3d职业规划与技能职级体系
  4. 饥荒自建服务器怎么换人物,饥荒怎么更换人物 饥荒换服务器方法大全
  5. 机动车驾驶员考试系统(科目二、三)数据采集卡介绍
  6. java+mysql 基于ssm的主题酒店预定入住管理系统
  7. c语言8 8点阵,共阴共阳的疑问解答以及8*8LED点阵基础知识讲解
  8. 梦话西游显示服务器,梦幻西游:梦幻公测后上线的第一批服务器,现在只剩下五个了...
  9. [SolidWorks二次开发]特征造型——拉伸(2)
  10. 15款图片批量处理软件下载