• SQLNu - Wednesday, December 27, 2017 5:53 PM

    Sue_H - Wednesday, December 27, 2017 5:41 PM

    You have the cte doing a select 1 so it's only selecting one row with the most recent laststatuschecktime.
    Try removing the top 1 and filter by computer name outside of the cte

    Sue

    Hi Sue, 
    I need to use the TOP 1 with ORDER BY so i can get only one value per machine for the status.Only way i was able to get this to work.

    Although you didn't provide any data or ddl, I am guessing it's because you have multiple status checks for each machine and you only want the most recent one. And in that case, no you don't need to use top 1.  If that's what you need and need it by computer name, you don't even need a CTE. But the requirements aren't nearly clear enough. If you just want to query the table for the last status check for a computer and there are multiple computers and multiple status checks for each, it would just be something like MAX(UCS.LastStatusCheckTime) and group by the other columns. But again, no data or information on the other columns.
    If you want a CTE (doesn't sound like it's needed) you could just include a row_number function and get a row number for every computer ordered by last status check. Inside the CTE you would add something like ROW_NUMBER() OVER(Partition by Name0 order by LastStatusCheckTimeDesc) as RowNum
    Then when you query the CTE, it would just be where RowNum = 1 to get the most recent status check time.

    Sue