sql中的函数

示例一

/*
传入参数@timeint类型,格式为hhmmss
返回值@backint类型,格式为hh00, hh15, hh30, hh45
时间time存储格式为int,含义为hhmmss,remainder为小时除以后的余数
*/
create function m15(@time int)
returns int as
begin
declare @back int, @remainder int
select @remainder = ((@time/100)%100)%15
select @back = (@time/100 - @remainder)
return @back
end

函数调用:

/*
15分钟级话务量查询。根据用户选定的小区(cellid),日期(date),
开始时间(startHour),结束时间(endHour),计算得出每15分钟级
的平均话务量。
*/
create proc Minute15TraffInfo(@cellid int, @date int, @startHour int, @endHour int) as
begin
select minute15, avg(traff) as avgTraff
from (select cellid, date, time, (select dbo.m15(time)), traff
      from data) as temp(cellid, date, time, minute15, traff)
where cellid = @cellid
and date = @date
and time > @startHour and time < @endHour
group by minute15
end

存储过程调用

exec Minute15TraffInfo 9011, 20071014, 180000, 230000

结果:
这里写图片描述

示例二

为了让数据按每15分钟分组,实现如下函数:
/*
传入参数@timeint类型,格式为hhmmss
返回值@backint类型,格式为hh00, hh15, hh30, hh45
时间time存储格式为int,含义为hhmmss,remainder为小时除以后的余数
*/
create function m15(@time int)
returns int as
begin
declare @back int, @remainder int
select @remainder = ((@time/100)%100)%15
select @back = (@time/100 - @remainder)
return @back
end

函数调用:

/*
15分钟级话务量查询。根据用户选定的小区(cellid),日期(date),
开始时间(startHour),结束时间(endHour),计算得出每15分钟级
的平均话务量。
*/
create proc Minute15TraffInfo(@cellid int, @date int, @startHour int, @endHour int) as
begin
select minute15, avg(traff) as avgTraff
from (select cellid, date, time, (select dbo.m15(time)), traff
      from data) as temp(cellid, date, time, minute15, traff)
where cellid = @cellid
and date = @date
and time >= @startHour and time <= @endHour
group by minute15
end

存储过程调用:

调用:exec Minute15TraffInfo 9011, 20071014, 180000, 230000

结果:
这里写图片描述

示例三

实现计算两个小区间距离的函数:
/*
计算并返回本小区(cellID),与另一个小区(adjceCellID)的距离
公式:D=R * arccos( siny1siny2+cosy1cosy2cos(x1-x2) ) 
*/
create function dis (@cellID int, @adjceCellID int)
returns float as
begin
declare @x1 float, @y1 float, @x2 float, @y2 float, @dis float
select @x1 = CellLatitude from cell where cellid = @cellID
select @y1 = CellLongitude from cell where cellid = @cellID
select @x2 = CellLatitude from cell where cellid = @adjceCellID
select @y2 = CellLongitude from cell where cellid = @adjceCellID
select @dis = 111.12*acos( sin(@y1)*sin(@y2)+cos(@y1)*cos(@y2)*cos(@x1-@x2) )
return @dis
end

函数调用:

/*
计算并返回本小区(cellID),与另一/*
邻区查询。根据用户选定的小区(cellID),调用函数dis,计算该选定小区的邻区,
如果与该小区的距离距离<2km,则认为是邻区,并将邻区按照距离升序排列。
*/
create proc AdjceCellInfo(@cellID int) as 
begin
select CellID, (select dbo.dis (@cellID, CellID)) as as distance
from cell
where (select dbo.dis (@cellID, CellID)) < 2
order by (select dbo.dis (@cellID, CellID)) 
end

存储过程调用:

exec AdjceCellInfo 9011

效果:
这里写图片描述

列举了SQL语句中大部分常用的函数 Abs(number) 取得数值的绝对值。 Asc(String) 取得字符串表达式的第一个字符ASCII 码。 Atn(number) 取得一个角度的反正切值。 CallByName (object, procname, usecalltype,[args()]) 执行一个对象的方法、设定或传回对象的属性。 CBool(expression) 转换表达式为Boolean 型态。 CByte(expression) 转换表达式为Byte 型态。 CChar(expression) 转换表达式为字符型态。 CDate(expression) 转换表达式为Date 型态。 CDbl(expression) 转换表达式为Double 型态。 CDec(expression) 转换表达式为Decimal 型态。 CInt(expression) 转换表达式为Integer 型态。 CLng(expression) 转换表达式为Long 型态。 CObj(expression) 转换表达式为Object 型态。 CShort(expression) 转换表达式为Short 型态。 CSng(expression) 转换表达式为Single 型态。 CStr(expression) 转换表达式为String 型态。 Choose (index, choice-1[, choice-2, ... [, choice-n]]) 以索引值来选择并传回所设定的参数。 Chr(charcode) 以ASCII 码来取得字符内容。 Close(filenumberlist) 结束使用Open 开启的档案。 Cos(number) 取得一个角度的余弦值。 Ctype(expression, typename) 转换表达式的型态。 DateAdd(dateinterval, number, datetime) 对日期或时间作加减。 DateDiff(dateinterval, date1, date2) 计算两个日期或时间间的差值。 DatePart (dateinterval, date) 依接收的日期或时间参数传回年、月、日或时间。 DateSerial(year, month, day) 将接收的参数合并为一个只有日期的Date 型态的数据。 DateValue(datetime) 取得符合国别设定样式的日期值,并包含时间。 Day(datetime) 依接收的日期参数传回日 ....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值