Query in SQL Server (machines that are now in specific project)
-
I have a table including 3 fields which are Machine Name, Project Name, Date Time where Date Time indicates the exact time when machine was assigned to a project. Now I want to find machines which are now in a specific project (in fact it may be that one machine in different times be allocated to different projects). I want lasted specific project be latest project that machine assigned to it.
-
I have a table including 3 fields which are Machine Name, Project Name, Date Time where Date Time indicates the exact time when machine was assigned to a project. Now I want to find machines which are now in a specific project (in fact it may be that one machine in different times be allocated to different projects). I want lasted specific project be latest project that machine assigned to it.
-
I want all machines that now be in specific project , may machines "a", 2 days ago in project "1" and now be in project "2" and machines "b" now in project "1", when I wand machines in project "1" ,only "b" should be in result and Should not machine "a" be in result
-
I want all machines that now be in specific project , may machines "a", 2 days ago in project "1" and now be in project "2" and machines "b" now in project "1", when I wand machines in project "1" ,only "b" should be in result and Should not machine "a" be in result
-
I want all machines that now be in specific project , may machines "a", 2 days ago in project "1" and now be in project "2" and machines "b" now in project "1", when I wand machines in project "1" ,only "b" should be in result and Should not machine "a" be in result
Something like this should work:
WITH cteOrderdAssignments As
(
SELECT
MachineName,
ProjectName,
ROW_NUMBER() OVER ( PARTITION BY MachineName ORDER BY [DateTime] DESC ) As RowNumber
FROM
MyTable
)
SELECT
MachineName
FROM
cteOrderedAssignments
WHERE
RowNumber = 1
And
ProjectName = 'Web 8.0'
ORDER BY
MachineName
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Something like this should work:
WITH cteOrderdAssignments As
(
SELECT
MachineName,
ProjectName,
ROW_NUMBER() OVER ( PARTITION BY MachineName ORDER BY [DateTime] DESC ) As RowNumber
FROM
MyTable
)
SELECT
MachineName
FROM
cteOrderedAssignments
WHERE
RowNumber = 1
And
ProjectName = 'Web 8.0'
ORDER BY
MachineName
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Row_number() function in combination with common table expression is the best way indeed
In Word you can only store 2 bytes. That is why I use Writer.