Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Query in SQL Server (machines that are now in specific project)

Query in SQL Server (machines that are now in specific project)

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elina Ma
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • E Elina Ma

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Sounds like a simple query to me. Did I forget something or have I misread the question?

      SELECT TOP 1 MachineName
      FROM MyTable
      WHERE ProjectName = 'Web 8.0'
      ORDER BY [DateTime] DESC

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      E 1 Reply Last reply
      0
      • L Lost User

        Sounds like a simple query to me. Did I forget something or have I misread the question?

        SELECT TOP 1 MachineName
        FROM MyTable
        WHERE ProjectName = 'Web 8.0'
        ORDER BY [DateTime] DESC

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        E Offline
        E Offline
        Elina Ma
        wrote on last edited by
        #3

        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

        L Richard DeemingR 2 Replies Last reply
        0
        • E Elina Ma

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 8407653 wrote:

          I want all machines that now

          Change the Sql accordingly. I want coffee.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • E Elina Ma

            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

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            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

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            H 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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

              H Offline
              H Offline
              Herman T Instance
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups