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. Trap difference between two records...

Trap difference between two records...

Scheduled Pinned Locked Moved Database
data-structurescryptographyhelptutorial
4 Posts 3 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.
  • D Offline
    D Offline
    deep7
    wrote on last edited by
    #1

    hi, i need to compare two records of a table, the one with max date and the second highest one...so here need to somehow run a loop and trap the difference of each column and insert this diference in values in a hash table.. i can't get how to do this..could any one plz help me out here thanx

    C 1 Reply Last reply
    0
    • D deep7

      hi, i need to compare two records of a table, the one with max date and the second highest one...so here need to somehow run a loop and trap the difference of each column and insert this diference in values in a hash table.. i can't get how to do this..could any one plz help me out here thanx

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      deep7 wrote:

      i can't get how to do this..could any one plz help me out here

      What aspect? The getting the rows out of the database? The looping over the columns? What? Since you asked in the SQL forum, I'm gussing that it is the getting stuff out of the database aspect. To get the two rows in one result set use this:

      SELECT TOP 2 *
      FROM MyTable
      ORDER BY [date] DESC

      To get two result sets each with one row use this:

      SELECT TOP 1 *
      FROM MyTable
      ORDER BY [date] DESC;

      SELECT TOP 1 *
      FROM MyTable
      WHERE [date] NOT IN(SELECT MAX([date]) FROM MyTable)
      ORDER BY [date] DESC;

      Does this help?


      "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

      D R 2 Replies Last reply
      0
      • C Colin Angus Mackay

        deep7 wrote:

        i can't get how to do this..could any one plz help me out here

        What aspect? The getting the rows out of the database? The looping over the columns? What? Since you asked in the SQL forum, I'm gussing that it is the getting stuff out of the database aspect. To get the two rows in one result set use this:

        SELECT TOP 2 *
        FROM MyTable
        ORDER BY [date] DESC

        To get two result sets each with one row use this:

        SELECT TOP 1 *
        FROM MyTable
        ORDER BY [date] DESC;

        SELECT TOP 1 *
        FROM MyTable
        WHERE [date] NOT IN(SELECT MAX([date]) FROM MyTable)
        ORDER BY [date] DESC;

        Does this help?


        "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

        D Offline
        D Offline
        deep7
        wrote on last edited by
        #3

        Hi, thanks, but i wanted to know how to compare these two record which i get...its like i have to store the diffrence suppose the 'amount' value is not same, then store this in a hash table...how to compare the two records (I have to check for all columns) hope u understood.. thanks

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          deep7 wrote:

          i can't get how to do this..could any one plz help me out here

          What aspect? The getting the rows out of the database? The looping over the columns? What? Since you asked in the SQL forum, I'm gussing that it is the getting stuff out of the database aspect. To get the two rows in one result set use this:

          SELECT TOP 2 *
          FROM MyTable
          ORDER BY [date] DESC

          To get two result sets each with one row use this:

          SELECT TOP 1 *
          FROM MyTable
          ORDER BY [date] DESC;

          SELECT TOP 1 *
          FROM MyTable
          WHERE [date] NOT IN(SELECT MAX([date]) FROM MyTable)
          ORDER BY [date] DESC;

          Does this help?


          "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

          R Offline
          R Offline
          r stropek
          wrote on last edited by
          #4

          Hi! This query shows how to do that: select e1.EmployeeID, e1.BirthDate, cast(e1.BirthDate-( select top 1 e2.BirthDate from HumanResources.Employee e2 where e2.BirthDate You can try it with SQL Server's AdventureWorks sample DB. Rainer Stropek Visit my blog at http://www.cubido.at/Blog/tabid/176/BlogID/4/Default.aspx

          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