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. Change old store to new store

Change old store to new store

Scheduled Pinned Locked Moved Database
databasesql-servertutorialannouncement
3 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    Member 13338075
    wrote on last edited by
    #1

    I want to change the old store to new store by using the query in MSSQL. For example,

    Staff_Id Name Transaction_Date New_Store Old_Store

    1 AA 1.1.2017 1

    1 AA 1.2.2017 2

    1 AA 1.3.2017 3

    1 AA 1.4.2017 1

    1 AA 1.5.2017 4

    I want to change the table to following like that
    Staff_Id Name Transaction_Date New_Store Old_Store

    1 AA 1.1.2017 1 NULL

    1 AA 1.2.2017 2 1

    1 AA 1.3.2017 3 2

    1 AA 1.4.2017 1 3

    I wrote the query like that, but it's not true, it's output randomly

    Update EC1 SET EC1.Old_Store=EC.New_Store
    From Employee_Change EC , Employee_Change EC1
    WHERE EC.Staff_Id=EC1.Staff_Id
    AND EC.Transaction_Date>=EC1.Transaction_Date

    Richard DeemingR CHill60C 2 Replies Last reply
    0
    • M Member 13338075

      I want to change the old store to new store by using the query in MSSQL. For example,

      Staff_Id Name Transaction_Date New_Store Old_Store

      1 AA 1.1.2017 1

      1 AA 1.2.2017 2

      1 AA 1.3.2017 3

      1 AA 1.4.2017 1

      1 AA 1.5.2017 4

      I want to change the table to following like that
      Staff_Id Name Transaction_Date New_Store Old_Store

      1 AA 1.1.2017 1 NULL

      1 AA 1.2.2017 2 1

      1 AA 1.3.2017 3 2

      1 AA 1.4.2017 1 3

      I wrote the query like that, but it's not true, it's output randomly

      Update EC1 SET EC1.Old_Store=EC.New_Store
      From Employee_Change EC , Employee_Change EC1
      WHERE EC.Staff_Id=EC1.Staff_Id
      AND EC.Transaction_Date>=EC1.Transaction_Date

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Something like this should work:

      UPDATE
      EC
      SET
      Old_Store =
      (
      SELECT TOP 1 New_Store
      FROM Employee_Change As EC1
      WHERE EC1.Staff_Id = EC.Staff_Id
      And EC1.Transaction_Date < EC.Transaction_Date
      ORDER BY EC1.Transaction_Date DESC
      )
      FROM
      Employee_Change As EC
      ;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • M Member 13338075

        I want to change the old store to new store by using the query in MSSQL. For example,

        Staff_Id Name Transaction_Date New_Store Old_Store

        1 AA 1.1.2017 1

        1 AA 1.2.2017 2

        1 AA 1.3.2017 3

        1 AA 1.4.2017 1

        1 AA 1.5.2017 4

        I want to change the table to following like that
        Staff_Id Name Transaction_Date New_Store Old_Store

        1 AA 1.1.2017 1 NULL

        1 AA 1.2.2017 2 1

        1 AA 1.3.2017 3 2

        1 AA 1.4.2017 1 3

        I wrote the query like that, but it's not true, it's output randomly

        Update EC1 SET EC1.Old_Store=EC.New_Store
        From Employee_Change EC , Employee_Change EC1
        WHERE EC.Staff_Id=EC1.Staff_Id
        AND EC.Transaction_Date>=EC1.Transaction_Date

        CHill60C Offline
        CHill60C Offline
        CHill60
        wrote on last edited by
        #3

        This has been answered at Change old store to new store[^] In future, do not cross-post across forums. Pick one and be patient - we do this in our spare time for free - sometimes it can take a little while :)

        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