作业帮 > 综合 > 作业

sql查询语句相关问题

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/12 15:49:16
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元之间的用户。
2、 查找2010年以来(截止到10月31日)所有后四位尾数符合AABB或者ABAB或者AAAA的电话号码。(A、 B 分别代表1—9中任意的一个数字)
3、 删除jifei表中所有10月份出现的两条相同记录中的其中一条记录。
4、查询所有9月份、10月份月均使用金额在30元以上的用户号码(结果不能出现重复)
我这是以oracle为例的.. .. ..
有些可能繁琐,但可以保证结果准确
楼上的第一题有误,只用话费多少是不可能精准定位出哪个电话号码在那几个月中花费那么多
1[0].select distinct t1.phone from
( select * from jifei
where to_date(month,'yyyymm') = to_date('2011-06','yyyy-mm') and expenses > 50 and expenses < 100 ) t1,
( select * from jifei
where to_date(month,'yyyymm') = to_date('2011-07','yyyy-mm') and expenses > 50 and expenses < 100 ) t2,
(select * from jifei
where to_date(month,'yyyymm') = to_date('2011-08','yyyy-mm') and expenses > 50 and expenses < 100 ) t3,
(
select * from jifei
where to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm') and expenses = 0 ) t4,
(
select * from jifei
where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses = 0 ) t5
where t1.phone = t2.phone
and t2.phone = t3.phone
and t3.phone = t4.phone
and t4.phone = t5.phone
1[1].select phone from
( select phone , 1 ct from jifei
where (to_date(month,'yyyymm') = to_date('2011-06','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-07','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-08','yyyy-mm')
and expenses > 50 and expenses < 100 )
or ( to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses = 0 )
group by phone having sum(ct) = 5
2. select distinct phone from (
select phone ,
substr(phone,length(phone)-3,1) fst,
substr(phone,length(phone)-2,1) sec,
substr(phone,length(phone)-1,1) thr,
substr(phone,length(phone),1) fou
from jifei
where to_date(month,'yyyymm') > todate ('201101','yyyymm') - 1/24/60/60/60
and to_date(month,'yyyymm') < todate ('201111','yyyymm') + 1/24/60/60/60
)
where (fst = sec and sec = thr and thr = fou)
or (fst = sec and thr = fou )
or (fst = thr and sec = fou )
--oracle 可以直接使用rowid,并不是没有办法
3. delete from jifei where rowid in (
select max(rowid) from jifei where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm')
group by phone,month,expenses having count(*) = 2
)
--其它数据库估计可以这样,不过用一条sql写不完
create table temp as select * from jifei where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm')
group by phone,month,expenses having count(*) = 2
)
truncate table jifei;
insert into jifei select * from temp;
drop table temp;
4. select distinct t1.phone from
(select phone,month from jifei
where to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm') and expenses > 30) t1,
(select phone,month from jifei
where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses > 30) t2
where t1.phone = t2.phone