sql count

  • I have a table like the following:

    Name Time1 Time2 Time3

    leo 06:03 0615 08:13

    John 06:17 10:03 11:05

    Can anyone tell me how to use sql to count the time between 06:00 and 06:30 and make it like the following:

    Name Time1 Time2 Time3 Total

    leo 06:03 0615 08:13 2

    John 06:17 10:03 11:05 1

    thanks in advanced

  • option 1 :

    create a CURSOR.For each row in the table, read the columns into varibales and check for the range and then update a counter,later update the counter before jumping on to the next row.

    option2:

    use CASE statement to update the counter Column which whill be something like:

    update

    set =

    case when [time1 & time2&time3 in range] then 3

    when [time1 & time2 in range] OR [time 1 & time3 in range] OR [time2 &3 in range] then 2,... like wise ..

  • leostrut (5/1/2013)


    I have a table like the following:

    Name Time1 Time2 Time3

    leo 06:03 0615 08:13

    John 06:17 10:03 11:05

    Can anyone tell me how to use sql to count the time between 06:00 and 06:30 and make it like the following:

    Name Time1 Time2 Time3 Total

    leo 06:03 0615 08:13 2

    John 06:17 10:03 11:05 1

    thanks in advanced

    So, what you're saying is that you have the following sample data: -

    IF object_id('tempdb..#testEnvironment') IS NOT NULL

    BEGIN

    DROP TABLE #testEnvironment;

    END;

    SELECT Name, Time1, Time2, Time3

    INTO #testEnvironment

    FROM (VALUES('leo', '06:03', '06:15', '08:13'),

    ('John', '06:17', '10:03', '11:05')

    )a(Name, Time1, Time2, Time3);

    Name Time1 Time2 Time3

    ---- ----- ----- -----

    leo 06:03 06:15 08:13

    John 06:17 10:03 11:05

    And from that, your expected result is: -

    Name Time1 Time2 Time3 Total

    ---- ----- ----- ----- -----------

    leo 06:03 06:15 08:13 2

    John 06:17 10:03 11:05 1

    In all honesty, I can't really see a relationship between "Total" and the "Time" columns, unless your "Total" is just a count of the number of values that are between 6:00 and 6:59 😛

    Can you provide more details as to why you expect the results that you've stated please?

    Also, please note how I've laid out the sample data that you provided. Now, anyone that uses SQL Server 2008 (which is the version for this specific forum section) can copy and paste that in to SSMS and have a copy of your sample data to play with. This allows people to focus on your question rather than on setting up test environments so results in more people being willing to help you.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply