早教吧作业答案频道 -->其他-->
sql查询语句相关问题有一个计费表表名jifei字段如下:phone(8位的电话号码),month(月份),expenses(月消费,费用为0表明该月没有产生费用)下面是该表的一条记录:64262631,201011,30.6这
题目详情
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元以上的用户号码(结果不能出现重复)
有一个计费表 表名 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
有些可能繁琐,但可以保证结果准确
楼上的第一题有误,只用话费多少是不可能精准定位出哪个电话号码在那几个月中花费那么多
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
看了 sql查询语句相关问题有一个...的网友还看了以下:
物理中动能的变化量公式是以下哪一条?E=0.5m(V2-V1)^2还是E=0.5mV2^2-0.5 2020-05-17 …
要证明线面垂直,是不是证明一条线垂直于品面内的一条直线就可以了 2020-06-03 …
证明一条直线垂直于三角形的两条边,则它一定垂直于三角形的第三条边要用直线与平面垂直的地理证明 2020-06-15 …
如何证明一条线段上的点与正方形上的点一样多?不是语言说明!康托尔证明过! 2020-06-20 …
人体体细胞中一条染色体怎么有2条姐妹染色单体?那我看到书上就是那样的,成对的染色体是2个叉叉。这不 2020-07-11 …
阅读下面的文字,完成题目。(选做题)有一个一无所长、生活无聊的年轻人,希望哲人能够给他指明一条道路 2020-07-12 …
如何证明一条线是一个角的角平分线,需要什么条件,我就知道被平分的两个角相等,这条线就是角平分线 2020-07-18 …
怎样说明一条直线是已知线段的垂直平分线或者说是怎样判断、证明一条直线是已知线段的中垂线 2020-07-29 …
请问怎么证明一条线是一条直线并且是180度RT在很多题目中,有许多看上去很像直线的线,但是实际上, 2020-07-30 …
以一条直线L上n个已知点为端点的射线的总条数S与已知点的个数n的关系式(请说明一条直线上n个已知点为 2020-12-09 …