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. query needed to swap column values for duplicate

query needed to swap column values for duplicate

Scheduled Pinned Locked Moved Database
databasehelp
6 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.
  • X Offline
    X Offline
    xcavin
    wrote on last edited by
    #1

    userid status phone_number
    1 0 111
    2 1 111
    3 0 222
    4 1 222
    5 0 333
    6 1 444

    For the above table I want write a SQL query to swap the value of "status" column for duplicate rows. Duplicates can be identified if phone number is same. The output would look like as below (affecting only 1,2,3 and 4).

    userid status phone_number
    1 1 111
    2 0 111
    3 1 222
    4 0 222
    5 0 333
    6 1 444

    Any help is appreciated. I always find it always hard to write even a simple SQL query :(.

    _ 1 Reply Last reply
    0
    • X xcavin

      userid status phone_number
      1 0 111
      2 1 111
      3 0 222
      4 1 222
      5 0 333
      6 1 444

      For the above table I want write a SQL query to swap the value of "status" column for duplicate rows. Duplicates can be identified if phone number is same. The output would look like as below (affecting only 1,2,3 and 4).

      userid status phone_number
      1 1 111
      2 0 111
      3 1 222
      4 0 222
      5 0 333
      6 1 444

      Any help is appreciated. I always find it always hard to write even a simple SQL query :(.

      _ Offline
      _ Offline
      _Damian S_
      wrote on last edited by
      #2

      You will need to do it in two parts... First, create a query that returns duplicate phone numbers, like this:

      select phone_number
      from TABLENAME
      group by phone_number
      having count(phone_number) > 1

      Then, you can use some white-man-magic to reverse your statii (assuming you want to switch 0 for 1, and 1 for 0), like this:

      update TABLENAME
      set status = abs(status - 1) -- This means if status is 0, you get abs(-1) which = 1
      where phone_number in
      (
      select phone_number
      from TABLENAME
      group by phone_number
      having count(phone_number) > 1
      )

      See how we use the first query to restrict the update query?

      I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! Booger Mobile (n) - A bright green 1964 Ford Falcon - our entry into the Camp Quality esCarpade!! Do something wonderful - make a donation to Camp Quality today!!

      X L 2 Replies Last reply
      0
      • _ _Damian S_

        You will need to do it in two parts... First, create a query that returns duplicate phone numbers, like this:

        select phone_number
        from TABLENAME
        group by phone_number
        having count(phone_number) > 1

        Then, you can use some white-man-magic to reverse your statii (assuming you want to switch 0 for 1, and 1 for 0), like this:

        update TABLENAME
        set status = abs(status - 1) -- This means if status is 0, you get abs(-1) which = 1
        where phone_number in
        (
        select phone_number
        from TABLENAME
        group by phone_number
        having count(phone_number) > 1
        )

        See how we use the first query to restrict the update query?

        I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! Booger Mobile (n) - A bright green 1964 Ford Falcon - our entry into the Camp Quality esCarpade!! Do something wonderful - make a donation to Camp Quality today!!

        X Offline
        X Offline
        xcavin
        wrote on last edited by
        #3

        That was real quick! thank you!

        _ 1 Reply Last reply
        0
        • X xcavin

          That was real quick! thank you!

          _ Offline
          _ Offline
          _Damian S_
          wrote on last edited by
          #4

          No worries! Thanks for your feedback...

          I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! Booger Mobile (n) - A bright green 1964 Ford Falcon - our entry into the Camp Quality esCarpade!! Do something wonderful - make a donation to Camp Quality today!!

          1 Reply Last reply
          0
          • _ _Damian S_

            You will need to do it in two parts... First, create a query that returns duplicate phone numbers, like this:

            select phone_number
            from TABLENAME
            group by phone_number
            having count(phone_number) > 1

            Then, you can use some white-man-magic to reverse your statii (assuming you want to switch 0 for 1, and 1 for 0), like this:

            update TABLENAME
            set status = abs(status - 1) -- This means if status is 0, you get abs(-1) which = 1
            where phone_number in
            (
            select phone_number
            from TABLENAME
            group by phone_number
            having count(phone_number) > 1
            )

            See how we use the first query to restrict the update query?

            I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! Booger Mobile (n) - A bright green 1964 Ford Falcon - our entry into the Camp Quality esCarpade!! Do something wonderful - make a donation to Camp Quality today!!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            No need for abs: new = 1 - old suffices. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            _ 1 Reply Last reply
            0
            • L Luc Pattyn

              No need for abs: new = 1 - old suffices. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              _ Offline
              _ Offline
              _Damian S_
              wrote on last edited by
              #6

              True... :sigh: :laugh:

              I don't have ADHD, I have ADOS... Attention Deficit oooh SHINY!! Booger Mobile (n) - A bright green 1964 Ford Falcon - our entry into the Camp Quality esCarpade!! Do something wonderful - make a donation to Camp Quality today!!

              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