Jump to content

Nasevz

Members
  • Posts

    7
  • Joined

  • Last visited

Posts posted by Nasevz

  1. You know I hadn't even thought of using subqueries, but that would work, I just tried it myself, and it seemed to do the trick, but try it yourself to make sure....here you go:
    SELECT distinct A.computerName, A.userName, A.Logontime, A.logofftimeFROM log AWHERE A.logontime = (SELECT max(b.logontime)     FROM log B     WHERE B.computerName = A.computerName)

    All this should do is pull back the data from the row where the max(logontime) exists for each computerName.  I include the B.computerName = A.computerName so that it will return the max for each computerName rather than just the max for the entire table.Let me know if you have any questions.

    If I understand your query correctly, you are suggesting me to have two identical tables with different names?
  2. The problem is that I would like whole row with all columns for each computename where max(logontime) has highest value and max(logofftime) is not necessarily in the same row with max(logontime) because sometimes workstations fail to write logofftime (reset, power failure ...). Maybe it can be written with subqueries, but I am not very experianced in SQL.Thank you for trying to help me.

  3. SELECT distinct computerName, MAX(logonTime)FROM tablename GROUP BY computerName

    This will first group all the computerNames and keep just one of each (distinct), then from that group it will find the max logon time.

    From previous message: select computername, max(logontime) from log group by computernamewould return the right values, but I need a whole row (including userid and logofftime), and if I add userID and logofftime to select statemet, they also must be either in agregate function or in a group by clause. I dont know if it is an agregate function that returns the value retreived by finding the max value of another column (return logofftime and userID from the row where logontime has maximum value) BTW grouping by computername implys distinct computernames, so with or without distinct, the result is the same.
  4. The query I submited should return the results you want. Lets break it down:
    SELECT distinct computerName, MAX(logonTime)FROM tablenameORDER BY logonTime DESC

    This is the error message from the select statementMsg 8120, Level 16, State 1, Line 1Column 'log.computername' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 145, Level 15, State 1, Line 1ORDER BY items must appear in the select list if SELECT DISTINCT is specified.1. distinct is applyed to whole row, not on computername only, so 'distinct computername, logontime' would return all distinct combinations of them.2. MAX(logontime) returns the maximum value of logontime for whole table and it is only one.If distinct computername returns 20 computernames and MAX(logontime) only one, the right rows can't be returnedselect computername, max(logontime) from log group by computernamewould return the right values, but I need a whole row (including userid and logofftime), and if I add userID and logofftime to select statemet, they also must be either in agregate function or in a group by clause. I dont know if it is an agregate function that returns the value retreived by finding the max value of another column (return logofftime and userID from the row where logontime has maximum value)
  5. something like this shuld work:
    SELECT distinct computerName, logonTimeFROM tablenameORDER BY logonTime DESC

    This would select all logontimes for all computers because all logontimes would most likely be different. I would like only the logontime that has the highest value for each computer.The computer names in the table are repeated many times (each time somebody logs on on that computer and there are many computers, not one) and I would like only the last logons for all computers.
  6. My table has this columns:rowId, computerName, logonTime, logoffTime, userNameI would like to select a row for each computerName where logonTime has highest value (To see rows where last logon happened for each computer).I am trying something like SELECT computerName, max(logonTime), logoffTime, userName from log GROUP BY computerName HAVING logonTime=max(logonTime) ???but I need to get row where max(logonTime) appears in each group (computerName).Can somebody help me?

×
×
  • Create New...