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

  • SEARCH
  • Home
  • 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 8503415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:45:10+00:00 2026-06-11T01:45:10+00:00

Sorry for the generic title of the question, but I didn’t know how else

  • 0

Sorry for the generic title of the question, but I didn’t know how else to put it.. So here goes:

I have a single table that holds the following information:

computerName | userName |           date          | logOn | startUp
             |          |                         |       |
ID_000000001 |  NULL    | 2012-08-14 08:00:00.000 |  NULL |   1
ID_000000001 |  NULL    | 2012-08-15 09:00:00.000 |  NULL |   0
ID_000000003 | user02   | 2012-08-15 19:00:00.000 |   1   |  NULL
ID_000000004 | user02   | 2012-08-16 20:00:00.000 |   0   |  NULL

computername and username are self-explanatory I suppose

logOn is 1 when the user logged on at the machine and 0 when he logged off.

startUp is 1 when the machine was turned on and 0 when it got turned off.

the other entry is alway NULL respectively since we can’t login and startup at the exact same time.

Now my task is: Find out which computers have been turned on the least amount of time over the last month (or any given amount of time, but for now let’s say one month) Is this even possible with SQL? <– Careful: I don’t need to know how many times a PC was turned on, but how many hours/minutes each computer was turned on over the given timespace

There’s two little problems as well:

We cannot say that the first entry of each computer is a 1 in the startUp column since the script that logs those events was installed recently and thus maybe a computer was already running when it started logging.

We cannot assume that if we order by date and only show the startUpcolumn that the entries will all be alternating 1’s and 0’s because if the computer is forced shut down by pulling the plug for example there won’t be a log for the shutdown and there could be two 1’s in a row.

EDIT: userName is of course NULL when startUp has a value, since turning on/shutting down doesn’t show which user did that.

  • 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-06-11T01:45:11+00:00Added an answer on June 11, 2026 at 1:45 am

    In a stored procedure, with cursors and fetch loops.
    And you use a temptable to store by computername the uptime.
    I give you the main plan, I’ll let you see for the details in the TSQL guide.

    Another link: a good example with TSQL Cursor.

     DECLARE @total_hour_by_computername    
    declare @computer_name varchar(255)
    declare @RowNum int
    
     --Now in you ComputerNameList cursor, you have all different computernames:
    declare ComputerNameList cursor for
    select DISTINCT computername from myTable
    
    -- We open the cursor
    OPEN ComputerNameList
    
         --You begin your foreach computername loop :
        FETCH NEXT FROM ComputerNameList 
        INTO @computer_name
        set @RowNum = 0 
        WHILE @@FETCH_STATUS = 0
        BEGIN
    
         SET @total_hour_by_computername=0;
    
         --This query selects all startup 1 dates by computername order by date.
         select @current_date=date from myTable where startup = 1 and computername = @computername order by date
    
             --You use a 2nd loop on the dates that were sent you back: 
    
             --This query gives you the previous date 
             select TOP(1) @previousDate=date from myTable 
             where computername = @computername and date < @current_date and startup is not null
             order by date DESC
    
    
             --If it comes null, you can decide not to take it into account.
             --Else
             SET @total_hour_by_computername=@total_hour_by_computername+datediff(hour, @previousDate, @current_date);
    
        --Once all dates have been parsed, you insert into your temptable the results for this computername
    
        INSERT INTO TEMPTABLE(Computername,uptime) VALUES (@computername,@total_hour_by_computername)
    
    --End of the @computer_name loop
      FETCH NEXT FROM ComputerNameList 
        INTO @computer_name
    END
    CLOSE ComputerNameList
    DEALLOCATE ComputerNameList
    

    You only need a select into your temptable to determine which one of the computers has been up the most time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the bad title, but I have no idea how to put this
I'm sorry for the generic title of this question but I wish I was
Horrible title of this question, I know. Sorry. I have four divs similar to
I'm sorry for the poor title, but it is a very generic question I
Sorry if this is generic in nature, but I have a question that maybe
Sorry, I know this question is easy, but I don't know how to get
Sorry for the semi-generic title, but I'm still pretty new at rails and couldn't
Sorry for the long title, but it seems most descriptive for my question. Basically,
Sorry to ask sich a generic question, but I've been studying these and, outside
Sorry I know this is a generic question, I'll try to provide as much

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.