Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:56:13+00:00 2026-05-11T18:56:13+00:00

I have Table1 and I need to get it to look like Table2: Table1

  • 0

I have Table1 and I need to get it to look like Table2:

Table1

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

Table2

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

 

I wrote a sql user-defined functions like this:

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

When calling the UDF in my sql query, I achieve the correct result:

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),Date, 108))
    order by 
        dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date, 108)) asc 

But I don’t like this method; My dream code would look like this:

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 [TIME] 
    order by [TIME] asc 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:56:13+00:00Added an answer on May 11, 2026 at 6:56 pm

    You can join to your table like a view and have your function call there. That way you can call the group by and order by on the column from the view.

    select
        Count(Page) as VisitingCount,
        [Time]
    from
    (
        SELECT
            Page,
            Date,
            [user],
            dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date, 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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have two tables that look something like this: Table 1 Id |
I have a table in SQL Server that is structured like this: id Name
I have a table with a credit and debit column. I need to get
I need to get the td element of a table. I do not have
I have some elements that need to have the text inside editable, for this
I have two identical tables and need to copy rows from table to another.
I have a table on SQL2000 with a numeric column and I need the
I have a table of about a million rows and I need to update
I have a table of time-series data of which I need to find all
I have a table that contains maybe 10k to 100k rows and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.