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. Help in Query?? [modified]

Help in Query?? [modified]

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
7 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.
  • G Offline
    G Offline
    geekfromindia
    wrote on last edited by
    #1

    I need to get a value from previous row in result with any extracted row...:confused:? here's with an example

    Table structure
    empid EmpName Otherdetails
    1 Robert abcd
    2 Peter abcd
    1 Bob abcd

    Above given is history table with records added for any update or delete...so Robert was the record inserted to main table and Record was updated as bob. So we have two records in history table as above with same id and modifieddate to get order Now here Robert updated his name as Bob....now in result set I need to get a list of employees and their previous name like..

    Id NewName OldName
    1 Robert Null
    2 Peter Null
    1 Bob Robert

    Can I get this somehow? Any hint or suggestions welcome..

    Keep DotNetting!! GeekFromIndia

    modified on Wednesday, September 3, 2008 11:23 AM

    B A 2 Replies Last reply
    0
    • G geekfromindia

      I need to get a value from previous row in result with any extracted row...:confused:? here's with an example

      Table structure
      empid EmpName Otherdetails
      1 Robert abcd
      2 Peter abcd
      1 Bob abcd

      Above given is history table with records added for any update or delete...so Robert was the record inserted to main table and Record was updated as bob. So we have two records in history table as above with same id and modifieddate to get order Now here Robert updated his name as Bob....now in result set I need to get a list of employees and their previous name like..

      Id NewName OldName
      1 Robert Null
      2 Peter Null
      1 Bob Robert

      Can I get this somehow? Any hint or suggestions welcome..

      Keep DotNetting!! GeekFromIndia

      modified on Wednesday, September 3, 2008 11:23 AM

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Show here your update command to see how you make that update.


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      G 1 Reply Last reply
      0
      • B Blue_Boy

        Show here your update command to see how you make that update.


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        G Offline
        G Offline
        geekfromindia
        wrote on last edited by
        #3

        This is actually a history table...which will have all insert, update and delete operations recorded...this works through a trigger... so for every update there will be a new row added to the table... So now I have old name and updated name as different rows...i need to get both names together in a query...can be selected using empId and modified date...

        Keep DotNetting!! GeekFromIndia

        1 Reply Last reply
        0
        • G geekfromindia

          I need to get a value from previous row in result with any extracted row...:confused:? here's with an example

          Table structure
          empid EmpName Otherdetails
          1 Robert abcd
          2 Peter abcd
          1 Bob abcd

          Above given is history table with records added for any update or delete...so Robert was the record inserted to main table and Record was updated as bob. So we have two records in history table as above with same id and modifieddate to get order Now here Robert updated his name as Bob....now in result set I need to get a list of employees and their previous name like..

          Id NewName OldName
          1 Robert Null
          2 Peter Null
          1 Bob Robert

          Can I get this somehow? Any hint or suggestions welcome..

          Keep DotNetting!! GeekFromIndia

          modified on Wednesday, September 3, 2008 11:23 AM

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          You can if you have say a datemodified column

          select id, newname,oldname
          from emp e1
          left outer join emp e2 on e2.id = e1.id
          and e2.datemodified = (select max(datemodified) from emp e3 where e3.id = e2.id
          and e3.datemodified < e2.datemodified)

          I might have a bit wrong here, as I don't have sql server on this PC so can't test, but you get the idea - find the same id where the datemodified is the latest date for this id BUT less than the current record, using an out join as presumably not every record will have been changed. Hope this helps

          Bob Ashfield Consultants Ltd

          G 2 Replies Last reply
          0
          • A Ashfield

            You can if you have say a datemodified column

            select id, newname,oldname
            from emp e1
            left outer join emp e2 on e2.id = e1.id
            and e2.datemodified = (select max(datemodified) from emp e3 where e3.id = e2.id
            and e3.datemodified < e2.datemodified)

            I might have a bit wrong here, as I don't have sql server on this PC so can't test, but you get the idea - find the same id where the datemodified is the latest date for this id BUT less than the current record, using an out join as presumably not every record will have been changed. Hope this helps

            Bob Ashfield Consultants Ltd

            G Offline
            G Offline
            geekfromindia
            wrote on last edited by
            #5

            Well this query makes sense...but its giving all null for oldname...i guess something small we are missing here...

            Keep DotNetting!! GeekFromIndia

            1 Reply Last reply
            0
            • A Ashfield

              You can if you have say a datemodified column

              select id, newname,oldname
              from emp e1
              left outer join emp e2 on e2.id = e1.id
              and e2.datemodified = (select max(datemodified) from emp e3 where e3.id = e2.id
              and e3.datemodified < e2.datemodified)

              I might have a bit wrong here, as I don't have sql server on this PC so can't test, but you get the idea - find the same id where the datemodified is the latest date for this id BUT less than the current record, using an out join as presumably not every record will have been changed. Hope this helps

              Bob Ashfield Consultants Ltd

              G Offline
              G Offline
              geekfromindia
              wrote on last edited by
              #6

              got it...awesome... there was a small change though...last clause should be e3.datemodified < e1.datemodified

              select id, newname,oldnamefrom emp e1left outer join emp e2 on e2.id = e1.idand e2.datemodified = (select max(datemodified) from emp e3 where e3.id = e2.idand e3.datemodified < e1.datemodified)

              Thanks a lot...

              Keep DotNetting!! GeekFromIndia

              A 1 Reply Last reply
              0
              • G geekfromindia

                got it...awesome... there was a small change though...last clause should be e3.datemodified < e1.datemodified

                select id, newname,oldnamefrom emp e1left outer join emp e2 on e2.id = e1.idand e2.datemodified = (select max(datemodified) from emp e3 where e3.id = e2.idand e3.datemodified < e1.datemodified)

                Thanks a lot...

                Keep DotNetting!! GeekFromIndia

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #7

                Great. Did wonder, I usually test first but couldn't this time. Still, it got you where you wanted to go. :-D

                Bob Ashfield Consultants Ltd

                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