查询用户13701418965在未缴费的市话账单信息。
accountbill表中
id,账单时间,电话号码,类型,金额,缴费情况,缴费时间

电话号码,缴费情况,类型

sql语法:
select列from表名where查询条件
列:电话号码、金额
表:accountbill
条件:电话号码、未缴费、市话

语句:
select'用户'+phonenumber+'的欠费金额为'+cast(chargeasvarchar)+'元'
fromaccountbill
where
phonenumber='13701418965'and
ispaid='0'and
calltype='0'

char:固定长度的字符型char(10):在硬盘中会申请10个字符长度
varchar:可变长度的字符型varchar(50):最大在硬盘中申请50个字符长度,真正占用的空间是随着字符长度的变化而变化


案例需求:显示用户13701418965的2008年6月份的通话记录的通话时长
history-call表中包含了列:
id、通话起始、通话结束、电话号码、类型、账单id

电话号码、开始时间、结束时间
列:phonenumber、starttime、endtime
表:histroy-call
条件:
电话号码
2008年6月

select'用户'+phonenumber+'在2008年6月份的通话时长为'+
cast(datediff(ss,starttime,endtime)/60.0asvarchar(20))+
'分钟'
from"history-call"
where
phonenumber='13701418965'and
starttime>'2008-06-0100:00:00'and
endtime<'2008-07-0100:00:00'


案例需求:显示用户13701418965的2008年6月份的通话记录的通话时长(通话时长精确到分钟,不到一分钟的按一分钟计算.)
select'用户'+phonenumber+'在2008年6月份的通话时长为'+
cast(ceiling(datediff(ss,starttime,endtime)/60.0)asvarchar(20))+
'分钟'
from"history-call"
where
phonenumber='13701418965'and
starttime>'2008-06-0100:00:00'and
endtime<'2008-07-0100:00:00'


案例:
显示用户13701418965欠费账单中的市话费用
accountbill
id,账单时间,电话号码,类型,金额,缴费情况,缴费时间
电话号码、类型、金额、时间
列:电话号码、时间、金额
表:accountbill
条件:
电话号码
市话
每个月
欠费
select'用户'+phonenumber+'在'+cast(datepart(yy,accounttime)aschar(4))+'年'+cast(datepart(mm,accounttime)aschar(2))+'月的市话欠费金额为'+cast(sum(charge)asvarchar)+'元'
fromaccountbill
wherephonenumber='13701418965'and
ispaid='0'and
calltype='0'----(错误,因为使用了sum)


案例需求:用户13701418965假设在2008-10-3115:20:00缴费时,计算用户账单欠费的滞纳金。
滞纳金=超期天数*欠费金额*0.01
超期天数=缴费日期-账单日下个月的26号

selectsum(ceiling(datediff(ss,cast(datepart(yy,accounttime)asvarchar)+'-'+cast(datepart(mm,dateadd(mm,1,accounttime))asvarchar)+'-2600:00:00','2008-10-3115:20:00')/86400.0)*charge*0.01)
fromaccountbill
where
phonenumber='13701418965'and
ispaid='0'
思路:
滞纳金最难算,所以我先算滞纳金
滞纳金里面超期天数最难算,那我们先算超期天数
超期天数里账单日下个月的26号最难算,所以我们先算账单日下个月的26号