加入收藏 | 设为首页 | 会员中心 | 我要投稿 东营站长网 (https://www.0546zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 如何调用用户定义函数以与select,group by,order by一起

发布时间:2021-02-20 19:46:21 所属栏目:MsSql教程 来源:网络整理
导读:我有Table1,我需要让它看起来像Table2: 表格1 VisitingCount | Date----------------------- 1 | 15:09 3 | 15:10 7 | 15:15 1 | 15:39 2 | 15:40 3 | 15:47 表2 VisitingCount | Date----------------------------- 11 | 15:00-15:30 6 | 15:30-16:00 我写

我有Table1,我需要让它看起来像Table2:

表格1

VisitingCount |  Date
-----------------------
      1       |  15:09
      3       |  15:10
      7       |  15:15
      1       |  15:39
      2       |  15:40
      3       |  15:47

表2

VisitingCount |  Date
-----------------------------
     11       |  15:00-15:30
     6        |  15:30-16:00

我写了一个像这样的SQL用户定义的函数:

create FUNCTION [dbo].[fn_GetActivityLogsArranger] (@time AS nvarchar(max))
RETURNS nvarchar(max)
AS
BEGIN
    declare @Return varchar(30)

    select @Return = 
        case 
            when @time between '15:00' and '15:30' then '15:00-15:30'
            when @time between '15:30' and '16:00' then '15:30-16:00'
            when @time between '16:00' and '16:30' then '16:00-16:30'
            when @time between '16:00' and '16:30' then '16:00-16:30' 
            when @time between '16:30' and '17:00' then '16:30-17:00' 
            when @time between '17:00' and '17:30' then '17:00-17:30' 
            when @time between '17:30' and '18:00' then '17:30-18:00'
            else 'Unknown'
        end

    return @Return
end

在我的SQL查询中调用UDF时,我得到了正确的结果:

select 
        Count(Page) as VisitingCount,dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date,108))
            as [Time] 
    from 
        scr_SecuristLog     
    where 
        Date between '2009-04-30' and '2009-05-02' AND
        [user] in
        (
            select USERNAME               
            from scr_CustomerAuthorities 
            where customerID = Convert(varchar,4) and ID = Convert(varchar,43) 
        )    
    group by 
        dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),108))
    order by 
        dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),108)) asc

但我不喜欢这种方法;我的梦想代码看起来像这样:

select 
        Count(Page) as VisitingCount,108))
            as [TIME]
    from 
        scr_SecuristLog     
    where 
        Date between '2009-04-30' and '2009-05-02' and 
        user] in
        (
            select USERNAME               
            from scr_CustomerAuthorities 
            where customerID = Convert(varchar,43) 
        )    
    group by [TIME] 
    order by [TIME] asc

解决方法

你可以像一个视图一样加入你的桌子并在那里打电话.这样,您可以通过视图中的列调用组并按顺序排序.
select
    Count(Page) as VisitingCount,[Time]
from
(
    SELECT
        Page,[user],108)) as [Time]
    FROM
        scr_SecuristLog
) scr_SecuristLog2
where
    Date between '2009-04-30' and '2009-05-02'
and
    [user] in
(
    select
        USERNAME
    from
     scr_CustomerAuthorities
    where
        customerID=Convert(varchar,4)
    and
        ID=Convert(varchar,43)
)
group by
    [Time]
order by
    [Time] asc

(编辑:东营站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读