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. MSSQL : Selecting a value from a row that has a column with minimun creation time for given parameters

MSSQL : Selecting a value from a row that has a column with minimun creation time for given parameters

Scheduled Pinned Locked Moved Database
sql-server
4 Posts 2 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.
  • P Offline
    P Offline
    Prasad A
    wrote on last edited by
    #1

    Hi,
    I want to know if there is a simple and efficient way to select a value from a row that has a column with minimum creation time for the given parameters.
    Let us take a sample table named Patient

    PatientId Observation Time Value

    1 Temp 2014-02-19 03:55:00 35
    1 Temp 2014-02-19 03:45:00 37
    1 Weight 2014-02-19 03:40:00 60

    If i am given the PatientId and Observation, I need to retrieve the value field with minimum creation time
    For patient id 1 and observation Temp this would be Value 37
    since it has 2014-02-19 03:45:00 as minimum creation time

    G 1 Reply Last reply
    0
    • P Prasad A

      Hi,
      I want to know if there is a simple and efficient way to select a value from a row that has a column with minimum creation time for the given parameters.
      Let us take a sample table named Patient

      PatientId Observation Time Value

      1 Temp 2014-02-19 03:55:00 35
      1 Temp 2014-02-19 03:45:00 37
      1 Weight 2014-02-19 03:40:00 60

      If i am given the PatientId and Observation, I need to retrieve the value field with minimum creation time
      For patient id 1 and observation Temp this would be Value 37
      since it has 2014-02-19 03:45:00 as minimum creation time

      G Offline
      G Offline
      GuyThiebaut
      wrote on last edited by
      #2

      This should work:

      select top 1 Value
      from Patient
      where PatientId = 1
      and Observation = 'Temp'
      and Time in
      (
      select min(Time)
      from Patient
      where PatientId = 1
      and Observation = 'Temp'
      )

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      P 1 Reply Last reply
      0
      • G GuyThiebaut

        This should work:

        select top 1 Value
        from Patient
        where PatientId = 1
        and Observation = 'Temp'
        and Time in
        (
        select min(Time)
        from Patient
        where PatientId = 1
        and Observation = 'Temp'
        )

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        P Offline
        P Offline
        Prasad A
        wrote on last edited by
        #3

        Thanks. That seems to be simple. If i have numerous entries for the specific patiend id and observation, will the use of top be performance efficient ?

        G 1 Reply Last reply
        0
        • P Prasad A

          Thanks. That seems to be simple. If i have numerous entries for the specific patiend id and observation, will the use of top be performance efficient ?

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Top ensures that only one line is returned. You can remove the top 1 - however if more than one line is returned you will have to make a choice at some point if the values are different. Simply replace the top 1 with distinct if you know there will only be one value returned.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          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