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. SQL statement

SQL statement

Scheduled Pinned Locked Moved Database
databasequestion
8 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.
  • S Offline
    S Offline
    Syafiqah Zahirah
    wrote on last edited by
    #1

    Hello..i want to ask question. In my database there is a column for Date such as below: 2014-03-26 2014-03-25 2014-03-24 Does anyone know how I want to retrieve data value from a previous row with select statement.If I want the latest data, so the sql is like this ORDER BY Actuators.Date DESC";and the answer I get is 2014-03-26. But if I want to show the previous data value from previous row what can I do?The result will show date 2014-03-25. Tq

    P L T 3 Replies Last reply
    0
    • S Syafiqah Zahirah

      Hello..i want to ask question. In my database there is a column for Date such as below: 2014-03-26 2014-03-25 2014-03-24 Does anyone know how I want to retrieve data value from a previous row with select statement.If I want the latest data, so the sql is like this ORDER BY Actuators.Date DESC";and the answer I get is 2014-03-26. But if I want to show the previous data value from previous row what can I do?The result will show date 2014-03-25. Tq

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Which database system? SQL Server 2012? You want to get the second highest? Or both the highest and second-highest in one row?

      You'll never get very far if all you do is follow instructions.

      S 1 Reply Last reply
      0
      • S Syafiqah Zahirah

        Hello..i want to ask question. In my database there is a column for Date such as below: 2014-03-26 2014-03-25 2014-03-24 Does anyone know how I want to retrieve data value from a previous row with select statement.If I want the latest data, so the sql is like this ORDER BY Actuators.Date DESC";and the answer I get is 2014-03-26. But if I want to show the previous data value from previous row what can I do?The result will show date 2014-03-25. Tq

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

        Syafiqah Zahirah wrote:

        what can I do?

        ..SELECT TOP 2 :)

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

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          Which database system? SQL Server 2012? You want to get the second highest? Or both the highest and second-highest in one row?

          You'll never get very far if all you do is follow instructions.

          S Offline
          S Offline
          Syafiqah Zahirah
          wrote on last edited by
          #4

          Database system is SQL Server 2012. I just want to get the second highest in a column Date.

          P 1 Reply Last reply
          0
          • S Syafiqah Zahirah

            Database system is SQL Server 2012. I just want to get the second highest in a column Date.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            You could use ROW_NUMBER and select the one with a value of 2. Or something like:

            SELECT TOP(1) * FROM (
            SELECT TOP(2) StartTime FROM dbo.RunSummary ORDER BY StartTime DESC
            ) T ORDER BY StartTime

            Neither seems very elegant.

            You'll never get very far if all you do is follow instructions.

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              You could use ROW_NUMBER and select the one with a value of 2. Or something like:

              SELECT TOP(1) * FROM (
              SELECT TOP(2) StartTime FROM dbo.RunSummary ORDER BY StartTime DESC
              ) T ORDER BY StartTime

              Neither seems very elegant.

              You'll never get very far if all you do is follow instructions.

              S Offline
              S Offline
              Syafiqah Zahirah
              wrote on last edited by
              #6

              Tqvm :)

              1 Reply Last reply
              0
              • L Lost User

                Syafiqah Zahirah wrote:

                what can I do?

                ..SELECT TOP 2 :)

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

                S Offline
                S Offline
                Syafiqah Zahirah
                wrote on last edited by
                #7

                yes..it working! ;)

                1 Reply Last reply
                0
                • S Syafiqah Zahirah

                  Hello..i want to ask question. In my database there is a column for Date such as below: 2014-03-26 2014-03-25 2014-03-24 Does anyone know how I want to retrieve data value from a previous row with select statement.If I want the latest data, so the sql is like this ORDER BY Actuators.Date DESC";and the answer I get is 2014-03-26. But if I want to show the previous data value from previous row what can I do?The result will show date 2014-03-25. Tq

                  T Offline
                  T Offline
                  TeddyLi
                  wrote on last edited by
                  #8

                  You try to do this by the following code:

                  select max(Actuators.Date)
                  from table1
                  where Actuators.Date<(select max(Actuaors.Date) from t1);

                  i hope it can help you.

                  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