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. Selecting a row with least creation time without using top

Selecting a row with least creation time without using top

Scheduled Pinned Locked Moved Database
11 Posts 6 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 any way to retrieve an entire row that has a column with least creation time without using top.

    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 the row with minimum creation time
    For patient id 1 and observation Temp this would be the row
    1 Temp 2014-02-19 03:45:00 37
    since it has 2014-02-19 03:45:00 as minimum creation time

    J Kornfeld Eliyahu PeterK L Y 4 Replies Last reply
    0
    • P Prasad A

      Hi,
      I want to know if there is any way to retrieve an entire row that has a column with least creation time without using top.

      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 the row with minimum creation time
      For patient id 1 and observation Temp this would be the row
      1 Temp 2014-02-19 03:45:00 37
      since it has 2014-02-19 03:45:00 as minimum creation time

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      WITH FirstObservation AS (
      SELECT PatientID,Observation,Min(Time) Time
      FROM MyTable
      WHERE PatientID = @PatientID
      AND Observation = @Observation
      GROUP BY PatientID,Observation
      )
      SELECT PatientId,Observation,TIME,Value
      FROM MyTable m
      JOIN FirstObservation f
      ON m.PatientID = f.PatientID
      AND m.Observation = f.Observation
      AND m.Time = f.Time

      Wrong is evil and must be defeated. - Jeff Ello[^]

      1 Reply Last reply
      0
      • P Prasad A

        Hi,
        I want to know if there is any way to retrieve an entire row that has a column with least creation time without using top.

        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 the row with minimum creation time
        For patient id 1 and observation Temp this would be the row
        1 Temp 2014-02-19 03:45:00 37
        since it has 2014-02-19 03:45:00 as minimum creation time

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #3

        Why not using order by and top? Because that is can be fast and effective, specially when you have an index on time column? Or just because it's an exercise you got at school?

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        L J 2 Replies Last reply
        0
        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

          Why not using order by and top? Because that is can be fast and effective, specially when you have an index on time column? Or just because it's an exercise you got at school?

          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

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

          Kornfeld Eliyahu Peter wrote:

          Why not using order by and top?

          Just a guess; "it's too slow"

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

          Kornfeld Eliyahu PeterK S 2 Replies Last reply
          0
          • L Lost User

            Kornfeld Eliyahu Peter wrote:

            Why not using order by and top?

            Just a guess; "it's too slow"

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

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #5

            And nested select not? It's performance depends only on the proper indexing...

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            L 1 Reply Last reply
            0
            • L Lost User

              Kornfeld Eliyahu Peter wrote:

              Why not using order by and top?

              Just a guess; "it's too slow"

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

              S Offline
              S Offline
              Simon_Whale
              wrote on last edited by
              #6

              Damn you Eddy you made try to learn something today!

              Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

              1 Reply Last reply
              0
              • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                And nested select not? It's performance depends only on the proper indexing...

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

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

                Kornfeld Eliyahu Peter wrote:

                And nested select not?

                I did not say that :)

                Kornfeld Eliyahu Peter wrote:

                It's performance depends only on the proper indexing...

                ..and the amount of records, the amount of fields in a row, their size, and the speed of the harddisk. Ya reckon there's an index on the date? :)

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

                Kornfeld Eliyahu PeterK 1 Reply Last reply
                0
                • L Lost User

                  Kornfeld Eliyahu Peter wrote:

                  And nested select not?

                  I did not say that :)

                  Kornfeld Eliyahu Peter wrote:

                  It's performance depends only on the proper indexing...

                  ..and the amount of records, the amount of fields in a row, their size, and the speed of the harddisk. Ya reckon there's an index on the date? :)

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

                  Kornfeld Eliyahu PeterK Offline
                  Kornfeld Eliyahu PeterK Offline
                  Kornfeld Eliyahu Peter
                  wrote on last edited by
                  #8

                  Eddy Vluggen wrote:

                  and the amount of records, the amount of fields in a row, their size

                  Not quiet right... With indexed table there will be two page reads only (and if the index clustered only one), not matter what size the table is...

                  Eddy Vluggen wrote:

                  Ya reckon there's an index on the date

                  I do not think there is a table at all - it sound me like a school exercise...

                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                  "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                  1 Reply Last reply
                  0
                  • P Prasad A

                    Hi,
                    I want to know if there is any way to retrieve an entire row that has a column with least creation time without using top.

                    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 the row with minimum creation time
                    For patient id 1 and observation Temp this would be the row
                    1 Temp 2014-02-19 03:45:00 37
                    since it has 2014-02-19 03:45:00 as minimum creation time

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

                    Create a table called "Cheater", containing both PatientId and last updatetime. Set both from a trigger on the Patient-table - do an insert of it doesn't contain the patient-Id, otherwise simply update the time. Index the table on time, id. You now have an indexed int/date table for your lookup.

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

                    1 Reply Last reply
                    0
                    • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                      Why not using order by and top? Because that is can be fast and effective, specially when you have an index on time column? Or just because it's an exercise you got at school?

                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #10

                      Maybe it's just not for an SQLserver. If you want a generic query TOP is out of the question.

                      Wrong is evil and must be defeated. - Jeff Ello[^]

                      1 Reply Last reply
                      0
                      • P Prasad A

                        Hi,
                        I want to know if there is any way to retrieve an entire row that has a column with least creation time without using top.

                        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 the row with minimum creation time
                        For patient id 1 and observation Temp this would be the row
                        1 Temp 2014-02-19 03:45:00 37
                        since it has 2014-02-19 03:45:00 as minimum creation time

                        Y Offline
                        Y Offline
                        Yes Yes Yes
                        wrote on last edited by
                        #11

                        Hi, I am a user of MySql. In Mysql we can go for the query

                        select * from Patient where time=(select max(time) from Patient);

                        Hope it will work for you. Check n update.

                        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