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. Database updates

Database updates

Scheduled Pinned Locked Moved Database
databaseannouncement
9 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
    spankyleo123
    wrote on last edited by
    #1

    Hi, I want to update the column TID with the top most value of TID as long as MID is the same number. I want to do this (table after update) PID SID TID MID RID 3 0 0 4 1 3 NULL 0 4 2 3 NULL 0 4 3 3 NULL 0 4 4 3 NULL 0 4 5 3 NULL 0 4 6 3 NULL 0 4 7 3 4 4 8 1 3 NULL 4 8 2 3 NULL 4 8 3 3 NULL 4 8 4 Below is the actual table before update. PID SID TID MID RID 3 0 0 4 1 3 NULL NULL 4 2 3 NULL NULL 4 3 3 NULL NULL 4 4 3 NULL NULL 4 5 3 NULL NULL 4 6 3 NULL NULL 4 7 3 4 4 8 1 3 NULL NULL 8 2 3 NULL NULL 8 3 3 NULL NULL 8 4 Any suggestion/solutions deeply appreciated. Thanks

    L D 2 Replies Last reply
    0
    • S spankyleo123

      Hi, I want to update the column TID with the top most value of TID as long as MID is the same number. I want to do this (table after update) PID SID TID MID RID 3 0 0 4 1 3 NULL 0 4 2 3 NULL 0 4 3 3 NULL 0 4 4 3 NULL 0 4 5 3 NULL 0 4 6 3 NULL 0 4 7 3 4 4 8 1 3 NULL 4 8 2 3 NULL 4 8 3 3 NULL 4 8 4 Below is the actual table before update. PID SID TID MID RID 3 0 0 4 1 3 NULL NULL 4 2 3 NULL NULL 4 3 3 NULL NULL 4 4 3 NULL NULL 4 5 3 NULL NULL 4 6 3 NULL NULL 4 7 3 4 4 8 1 3 NULL NULL 8 2 3 NULL NULL 8 3 3 NULL NULL 8 4 Any suggestion/solutions deeply appreciated. Thanks

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

      Assuming your DB is SQL Server, you can try this (just out of mind, not tried though)

      UPDATE MyTable A SET A.TID = (SELECT MAX(B.TID) FROM MyTable B WHERE A.MID = B.MID)

      S 2 Replies Last reply
      0
      • L Lost User

        Assuming your DB is SQL Server, you can try this (just out of mind, not tried though)

        UPDATE MyTable A SET A.TID = (SELECT MAX(B.TID) FROM MyTable B WHERE A.MID = B.MID)

        S Offline
        S Offline
        spankyleo123
        wrote on last edited by
        #3

        hi thanks for your reply. But am afraid your SQL didnt work. All it did was updated the entire column with the max value in the TID column.

        1 Reply Last reply
        0
        • L Lost User

          Assuming your DB is SQL Server, you can try this (just out of mind, not tried though)

          UPDATE MyTable A SET A.TID = (SELECT MAX(B.TID) FROM MyTable B WHERE A.MID = B.MID)

          S Offline
          S Offline
          spankyleo123
          wrote on last edited by
          #4

          Thanks for your help. I have solved it by creating a view of all 'Not NULL' (top most) rows . Then created a stored proc to loop through the view and update the TID. A long procedure though but it did work. Cheers !

          1 Reply Last reply
          0
          • S spankyleo123

            Hi, I want to update the column TID with the top most value of TID as long as MID is the same number. I want to do this (table after update) PID SID TID MID RID 3 0 0 4 1 3 NULL 0 4 2 3 NULL 0 4 3 3 NULL 0 4 4 3 NULL 0 4 5 3 NULL 0 4 6 3 NULL 0 4 7 3 4 4 8 1 3 NULL 4 8 2 3 NULL 4 8 3 3 NULL 4 8 4 Below is the actual table before update. PID SID TID MID RID 3 0 0 4 1 3 NULL NULL 4 2 3 NULL NULL 4 3 3 NULL NULL 4 4 3 NULL NULL 4 5 3 NULL NULL 4 6 3 NULL NULL 4 7 3 4 4 8 1 3 NULL NULL 8 2 3 NULL NULL 8 3 3 NULL NULL 8 4 Any suggestion/solutions deeply appreciated. Thanks

            D Offline
            D Offline
            David Mujica
            wrote on last edited by
            #5

            I think you want to do something like this ...

            update table A
            set tid =
            (select max(tid)
            from TableA
            Group By mid)

            Check these for references: http://msdn.microsoft.com/en-us/library/ms177523.aspx http://data.bangtech.com/sql/sql\_update\_with\_correlated\_subquery.htm

            S 1 Reply Last reply
            0
            • D David Mujica

              I think you want to do something like this ...

              update table A
              set tid =
              (select max(tid)
              from TableA
              Group By mid)

              Check these for references: http://msdn.microsoft.com/en-us/library/ms177523.aspx http://data.bangtech.com/sql/sql\_update\_with\_correlated\_subquery.htm

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

              Hi thanks for your solution and links, But your query returned an error. Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

              J 1 Reply Last reply
              0
              • S spankyleo123

                Hi thanks for your solution and links, But your query returned an error. Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

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

                That's because he forgot a small but important condition. Try this:

                update table A as a1
                set tid =
                (select max(tid)
                from TableA as A2
                Where A1.mid = A2.mid
                Group By mid)

                Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                D 1 Reply Last reply
                0
                • J Jorgen Andersson

                  That's because he forgot a small but important condition. Try this:

                  update table A as a1
                  set tid =
                  (select max(tid)
                  from TableA as A2
                  Where A1.mid = A2.mid
                  Group By mid)

                  Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                  D Offline
                  D Offline
                  David Mujica
                  wrote on last edited by
                  #8

                  Thanks for the refinement to my SQL. Sometimes when you don't have the actual data to test with, you forget some stuff.

                  J 1 Reply Last reply
                  0
                  • D David Mujica

                    Thanks for the refinement to my SQL. Sometimes when you don't have the actual data to test with, you forget some stuff.

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

                    David Mujica wrote:

                    Sometimes when you don't have the actual data to test with, you forget some stuff.

                    Tell me about it. :rolleyes: That's almost something I can put in my CV.

                    Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

                    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