CREATE TABLE test1
(
CID INTEGER NOT NULL,
MAID INTEGER NOT NULL,
CALLINGNUM VARCHAR2(20 BYTE) NOT NULL, --呼叫号码
)insert into test1 values(1,1,'15800000000');
insert into test1 values(2,2,'15812345678');
insert into test1 values(3,3,'0075000');
insert into test1 values(3,4,'10086');CREATE TABLE test2
(
CID INTEGER NOT NULL
)insert into test2 values(1);
insert into test2 values(2);
insert into test2 values(3);
insert into test2 values(4);CREATE TABLE test3
(
MAID INTEGER NOT NULL,
PRICES NUMBER(5,2) NOT NULL--套餐费用
)insert into test3 values(1,10);
insert into test3 values(2,20);
insert into test3 values(3,30);
insert into test3 values(4,40);
CREATE TABLE test4
(
CALLINGNUM VARCHAR2(20 BYTE) NOT NULL,--呼叫号码
CONCOST NUMBER NOT NULL--通话费用
)insert into test4 values('15800000000',20);
insert into test4 values('15812345678',10);
insert into test4 values('0075000',30);
insert into test4 values('10086',40);
解决方案 »
- Oracle 数据导出导入问题
- 新手,求写一个Linux自动登陆ORACLE数据库的.sh文件
- 为什么我写的sql语句必需要把对象加上双引号才能执行?
- 如何让无记录的行返回0?
- oracle中,从一个表读出多条记录提交到另外一个表怎么实现??
- 存储过程中是否可以使用truncate table 来实现删除表数据?
- ORACLE的安装,我该选择哪一个?
- 急!ASP.net连接Oracle9I的问题
- 已设置Session和Process为480,300 实际连接30个Process就满了,如何查找哪里设置有问题?
- Kettle连接上了impala ,但是获取表列表时报错是怎么回事啊!
- 数据库字段怎么会自己变汉字
- 关于查询数据的整合
CREATE TABLE test1
(
CID INTEGER NOT NULL,
MAID INTEGER NOT NULL,
CALLINGNUM VARCHAR2(20 BYTE) NOT NULL, --呼叫号码
)insert into test1 values(1,1,'15800000000');
insert into test1 values(2,2,'15812345678');
insert into test1 values(3,3,'0075000');
insert into test1 values(3,4,'10086');CREATE TABLE test2
(
CID INTEGER NOT NULL
)insert into test2 values(1);
insert into test2 values(2);
insert into test2 values(3);
insert into test2 values(4);CREATE TABLE test3
(
MAID INTEGER NOT NULL,
PRICES NUMBER(5,2) NOT NULL--套餐费用
)insert into test3 values(1,10);
insert into test3 values(2,20);
insert into test3 values(3,30);
insert into test3 values(4,40);
CREATE TABLE test4
(
CALLINGNUM VARCHAR2(20 BYTE) NOT NULL,--呼叫号码
CONCOST NUMBER NOT NULL--通话费用
)insert into test4 values('15800000000',1);
insert into test4 values('15800000000',2);
insert into test4 values('15812345678',15);
insert into test4 values('15812345678',10);
insert into test4 values('0075000',30);
insert into test4 values('0075000',30);
insert into test4 values('10086',40);
insert into test4 values('10086',40);当呼叫号码以00或10086开头时,直接读取CONCOST;当呼叫号码不是以00或10086开头时,且通话费用大于套餐费用时读取concost的总和,当通话费用小于套餐费用时直接读取套餐费用,最后将3类费用相加。
需要的结果
callednum concost
15800000000 10
15812345678 25
00750000 60
10086 80
t1.callingnum,
case
when t1.callingnum like '00%' or t1.callingnum like '10086%' then
sum(t4.concost)
else
greatest(sum(t4.concost), t3.prices)
end
from test1 t1, test3 t3, test4 t4
where t1.maid = t3.maid(+)
and t1.callingnum = t4.callingnum
group by t1.cid, t1.CALLINGNUM, t3.prices
from (select t5.*,
case
when t5.r = 1 then
t5.concost
when t5.r = 1 and concost < prices then
t5.prices
else
t5.concost
end cost
from (select t4.callingnum,
prices,
concost,
decode(substr(t4.callingnum, 1, 2),
'00',
1,
decode(substr(t4.callingnum, 1, 5),
'10086',
1,
0)) r
from test1 t1, test2 t2, test3 t3, test4 t4
where t1.cid = t2.cid
and t3.maid = t1.maid
and t4.callingnum = t1.callingnum) t5)
group by callingnum;
from (select t5.*,
case
when t5.r = 1 then
t5.concost
when t5.r = 1 and concost < prices then
t5.prices
else
t5.concost
end cost
from (select t4.callingnum,
prices,
concost,
decode(substr(t4.callingnum, 1, 2),
'00',
1,
decode(substr(t4.callingnum, 1, 5),
'10086',
1,
0)) r
from test1 t1, test2 t2, test3 t3, test4 t4
where t1.cid = t2.cid
and t3.maid = t1.maid
and t4.callingnum = t1.callingnum) t5)
group by callingnum;3小于套餐费用10,应该读取10哦
from (select t5.*,
case
when t5.r = 1 then
t5.concost
when t5.r = 1 and concost < prices then
t5.prices
else
t5.concost
end cost
from (select t4.callingnum,
prices,
concost,
decode(substr(t4.callingnum, 1, 2),
'00',
1,
decode(substr(t4.callingnum, 1, 5),
'10086',
1,
0)) r
from test1 t1, test2 t2, test3 t3, test4 t4
where t1.cid = t2.cid
and t3.maid = t1.maid
and t4.callingnum = t1.callingnum) t5)
group by callingnum;3小于套餐费用10,应该读取10哦
理解都错了,更正如下:select t5.callingnum,
case
when t5.r = 1 then
t5.concost
when t5.r = 0 and concost < prices then
t5.prices
else
t5.concost
end cost
from (select t4.callingnum,
prices,
concost,
decode(substr(t4.callingnum, 1, 2),
'00',
1,
decode(substr(t4.callingnum, 1, 5), '10086', 1, 0)) r
from test1 t1,
test2 t2,
test3 t3,
(select callingnum, sum(concost) concost
from test4
group by callingnum) t4
where t1.cid = t2.cid
and t3.maid = t1.maid
and t4.callingnum = t1.callingnum) t5