全部版块 我的主页
论坛 提问 悬赏 求职 新闻 读书 功能一区 经管百科 爱问频道
5267 1
2016-03-28
最近在准备数据分析师的笔试,然后遇到了一些数据库的题目,以Oracle数据库为例。代码已经测试过可以使用,但是感觉写得太烦了,还请懂SQL得大神帮我改改。

sql查询语句相关问题

有一个计费表 表名 jifei字段如下: phone(8位的电话号码), month(月份),expenses(月消费,费用为0表明该月没有产生费用)

下面是该表的一条记录:64262631,201011,30.6这条记录的含义就是64262631的号码在2010年11月份产生了30.6元的话费。

按照要求写出满足下列条件的sql语句:

1、查找2010年6、7、8月有话费产生但9、10月没有使用并(6、7、8月话费均在51-100元之间的用户。

select distinctphone from jifei

where month='201006'and

expenses between 51and 100

and phone in (

select distinctphone from jifei

where month='201007'

and expenses between51 and 100)

and phone in (

select distinctphone from jifei

where month='201008'

and expenses between51 and 100)

and phone in (

select distinctphone from jifei

where month='201009'

and expenses =0)

and phone in (

select distinctphone from jifei

where month='201010'

and expenses =0)

;

2、查找2010年以来(截止到10月31日)所有后四位尾数符合AABB或者ABAB或者AAAA的电话号码。(A、 B 分别代表1—9中任意的一个数字)

select distinctphone from jifei

whereto_date(month,'yyyymm')<=to_date('201010 ','yyyymm')

and(substr(phone,5,1)=substr(phone,6,1)

andsubstr(phone,7,1)=substr(phone,8,1))

or(substr(phone,5,1)=substr(phone,7,1)

andsubstr(phone,6,1)=substr(phone,8,1))

or(substr(phone,5,1)=substr(phone,6,1)

andsubstr(phone,5,1)=substr(phone,7,1)

andsubstr(phone,5,1)=substr(phone,8,1)

andsubstr(phone,5,1)=substr(phone,9,1))

;

3、删除jifei表中所有10月份出现的两条相同记录中的其中一条记录。

方法:添加一列ID,作为主键

delete from jifei a

wheresubstr(a.month,5,2)='10'

and(a.phone,a.month) in

(select phone,monthfrom jifei

group by phone,month

havingcount(*)>1)

and a.id not in (

select min(id) fromjifei

group by phone,month

havingcount(*)>1) ;

4、查询所有9月份、10月份月均使用金额在30元以上的用户号码(结果不能出现重复)

select phone from (

selectphone,avg(expenses) as avg_exp from(

select distinct *from jifei

wheresubstr(month,5,2) in ('09','10'))

group by phone)

whereavg_exp>=30;


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-3-30 07:15:22
不错,收藏学习了,多谢楼主
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群