高手留步,電話費用結算sql,实属无奈才求救~!

wusongwtl 2007-06-20 12:36:40
有a,b兩表 a表含有两字段 flt,areacode 前者记录通话时长,后者记录电话打往地

b表 areacode(对应a表) ,stime,etime,cost
用areacode会找到三笔记录

areacode stime etime cost
0~3分钟 0.22元/m 000 0 3 0.22
3~5分钟 0.20元/m 000 3 5 0.20
超过5分钟 0.11元/m 000 5 无穷大 0.11

如何求电话费用?(最好能通用,因有的areacode计费标准可能只有一条记录)


...全文
268 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wusongwtl 2007-06-20
  • 打赏
  • 举报
回复
一点了,不要午休了,我急啊
winesmoke 2007-06-20
  • 打赏
  • 举报
回复
楼上正解
lwl0606 2007-06-20
  • 打赏
  • 举报
回复

create table a (flt int ,areacode nvarchar(10))
insert into a select 1,'000'
insert into a select 3,'000'
insert into a select 4,'000'
insert into a select 10,'000'


create table b (areacode nvarchar(10),stime int, etime int ,cost decimal(10,2))
insert into b select '000',0,3,0.22
insert into b select '000',3,5,0.20
insert into b select '000',5,999999,0.11

select a.flt,a.areacode,
sum((case when flt >= etime then etime-stime else (case when flt > stime then flt - stime end ) end )*cost) as 电话费
from a left outer join b on a.areacode=b.areacode and
(case when flt >= etime then etime else (case when flt > stime then flt end ) end ) between stime and etime
group by a.flt,a.areacode

flt areacode 电话费
----------- ---------- ----------------------------------------
1 000 .22
3 000 .66
4 000 .86
10 000 1.61

(4 row(s) affected)
lwl0606 2007-06-20
  • 打赏
  • 举报
回复
select a.flt,a.areacode,
(case when flt >= etime then etime else (case when flt > stime then flt - stime end ) end )*cost as 电话费
from a left outer join b on a.areacode=b.areacode
(case when flt >= etime then etime else (case when flt > stime then flt - stime end ) end ) between stime and etime


昵称被占用了 2007-06-20
  • 打赏
  • 举报
回复

declare @ta table(flt int,areacode varchar(10))
insert @ta
select 1,'000' union all
select 4,'000' union all
select 6,'000' union all
select 2,'000'
declare @tb table(areacode varchar(10),stime int,etime int,cost decimal(5,2))
insert @tb
select '000',0,3,0.22 union all
select '000',3,5,0.20 union all
select '000',5,NULL,0.11

select a.flt,a.areacode,sum(case when b.etime is null or a.flt between b.stime and b.etime then (a.flt-b.stime)*b.cost
else (b.etime-b.stime)*b.cost
end) as cost from @ta as a left join @tb as b
on a.flt > isnull(b.etime,b.stime) or a.flt between b.stime and b.etime
group by a.flt,a.areacode

--结果
flt areacode cost
----------- ---------- ----------------------------------------
1 000 .22
2 000 .44
4 000 .86
6 000 1.17

(所影响的行数为 4 行)
hellowork 2007-06-20
  • 打赏
  • 举报
回复
注释:

2.69 =
1*.22 /*flt = 1*/
+ 3*.22 + 1*.20 /*flt = 4*/
+ 3*.22 + 2*.20 + 1*.11 /*flt = 6*/
+ 2*.22 /*flt = 2*/
hellowork 2007-06-20
  • 打赏
  • 举报
回复
试一下这个,分段计算的:
declare @ta table(flt int,areacode varchar(10))
insert @ta
select 1,'000' union all
select 4,'000' union all
select 6,'000' union all
select 2,'000'
declare @tb table(areacode varchar(10),stime int,etime int,cost decimal(5,2))
insert @tb
select '000',0,3,0.22 union all
select '000',3,5,0.20 union all
select '000',5,NULL,0.11 /*注意:无限大时用NULL表示*/

SELECT areacode,sum(times*cost) as cost FROM
(select a.flt,a.areacode,times =
case
when a.flt between b.stime and b.etime then a.flt - b.stime
when a.flt > b.etime then b.etime - b.stime
when a.flt > b.stime and b.etime is null then a.flt - b.stime
end ,b.cost
from @ta as a inner join @tb as b
on a.flt > isnull(b.etime,b.stime) or a.flt between b.stime and b.etime) AS t
GROUP BY areacode

/*结果
areacode cost
----------------
000 2.69
*/
yrwx001 2007-06-20
  • 打赏
  • 举报
回复
create table #t (arecode varchar(10),money float)
decalre @stime int
select @stime = stime,@cost = cost*(flt-stime),
from a left outer join b on a.areacode=b.areacode and flt between stime and etime
insert #t
select areacode,money = sum((etime-stime)*cost) from a left outer join b on a.areacode=b.areacode and etime < @stime
insert #t
select areacode,money = cost(flt-stime)
from a left outer join b on a.areacode=b.areacode and flt between stime and etime

select areacode,sum(money) from #t group by areacode
wusongwtl 2007-06-20
  • 打赏
  • 举报
回复
想简单了吧,分段计算的
hellowork 2007-06-20
  • 打赏
  • 举报
回复
declare @ta table(flt int,areacode varchar(10))
insert @ta
select 1,'000' union all
select 4,'000' union all
select 6,'000' union all
select 2,'000'
declare @tb table(areacode varchar(10),stime int,etime int,cost decimal(5,2))
insert @tb
select '000',0,3,0.22 union all
select '000',3,5,0.20 union all
select '000',5,NULL,0.11

select a.areacode,sum(a.flt*b.cost) as cost from @ta as a left join @tb as b
on a.flt > isnull(b.etime,b.stime) or a.flt between b.stime and b.etime
group by a.areacode

/*结果
areacode cost
----------------
000 5.52
*/
肥胖的柠檬 2007-06-20
  • 打赏
  • 举报
回复
我想问一下,
0~3分钟 0.22元/m 000 0 3 0.22
3~5分钟 0.20元/m 000 3 5 0.20
超过5分钟 0.11元/m 000 5 无穷大 0.11

这个如果是打了11分钟,什么算? 11*0.11元/m ? 还是 0.22*2+0.20*2+0.11*7

如果是11*0.11 用lwl0606(寒泉) 有就可以了
lwl0606 2007-06-20
  • 打赏
  • 举报
回复
或者
select a.flt,a.areacode,flt*cost as 电话费
from a left outer join b on a.areacode=b.areacode and flt between stime and etime
lwl0606 2007-06-20
  • 打赏
  • 举报
回复


select a.flt,a.areacode,flt*cost as 电话费
from a left outer join b on a.areacode=b.areacode and flt >=stime and flt <=etime
易信电脑版是易信聊天软件的pc客户端。软件由网易与电信公司合作开发,是一款聊天社交软件,不过目前PC客户端仅支持Windows操作系统。软件拥有免费的短信和电话功能,可以帮助用户摆脱话费和网络限制。独家的高清语音技术、海量的免费贴图表情,音乐分享等功能。软件界面美观简洁、简单全面、实用方便,可快速上手,轻轻松松完成日常聊天社交功能,真正做到简单全面实用。是用户实现聊天社交功能的好帮手。 易信电脑版功能 你想说的,我都能听到 文字聊天、高清语音、已读回执,细节精心打磨,让聊天变得更爽; 真正好用的免费电话,通话过程不耗流量,人在国外也能打; TA不在线,消息转为免费短信,电话留言,一键直达。 多种极致沟通方式,想怎么聊就怎么聊! 让兴趣自由发声 全新群组上线,读书、健身、宠物、明星......迅速找到你感兴趣的群, 聊你想聊; 群空间,一个群好友的私密朋友圈,记录心情,分享照片,与你们有关的,不再错过。 与有意思的人不期而遇 精彩晒一晒,随时晒出自己的精彩; 一键偶遇,邂逅有缘人,与最适合的TA不期而遇,缘分有时真的很奇妙。 世界这么大,原来你也在这里; 遇见对味儿的人,搭建你专属的社交圈。 红包新玩法,由你自定义 红包功能火热登场,任性的土豪请留步,我们做朋友! 更有任务红包新奇玩法,定制你的红包任务,要红包?那就先爆张照片吧! 精品游戏,独特体验 网易系精品手游独家体验,易信用户专属礼包! 各类热门游戏,好友对战让你玩得停不下来,和无聊,说再见! 你的情绪,放肆表达 有时,一个表情,胜过千言万语; 阿鸡米德、熊小易、喵丝丽、章大嘴、雷兔、比格鲍斯...... 百变易信表情家族,爆笑演绎百种情绪, 耍帅扮酷,卖萌逗乐,最嗨表情只在这里! 易信电脑版截图

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧