We help IT Professionals succeed at work.
Get Started

How to set a column value based upon duplicate records

brgdotnet
brgdotnet asked
on
110 Views
Last Modified: 2015-02-26
Suppose I have the following data in a table named "Teams"
If I have a table of football teams.
I want to update the table and set the "Flag" column to 1, if there are any duplicate football teams in the table.
The TeamId will determine if a row is a duplicate, since the TeamId would occur more than once. Here is an example of the table before the update.

TeamId   TeamName                   Flag
BAL          Baltimore Ravens
BAL          Baltimore Ravens
BAL          Baltimore Ravens
PAT          NE Patriots
SF9           SF 49ers
SF9           SF 49ers
SF9           SF 49ers
SDC          San Diego Chargers

Here is how the table would look after the update. All duplicate  rows have their Flag set to a value of "1"


TeamId   TeamName                   Flag
BAL          Baltimore Ravens           1
BAL          Baltimore Ravens           1
BAL          Baltimore Ravens           1
PAT          NE Patriots  
SF9           SF 49ers                           1
SF9           SF 49ers                           1
SF9           SF 49ers                           1
SDC          San Diego Chargers




INSERT INTO Team (ID,FirstName,LastName) VALUES ('BAL', 'Ravens',null)
INSERT INTO Team (ID,FirstName,LastName) VALUES ('BAL', 'Ravens',null)
INSERT INTO Team (ID,FirstName,LastName) VALUES ('BAL', 'Ravens',null)

INSERT INTO Team (ID,FirstName,LastName) VALUES ('PAT', 'Patriots',null)

INSERT INTO Team (ID,FirstName,LastName) VALUES ('SF9', '9ers',null)
INSERT INTO Team (ID,FirstName,LastName) VALUES ('SF9', '9ers',null)
INSERT INTO Team (ID,FirstName,LastName) VALUES ('SF9', '9ers',null)
INSERT INTO Team (ID,FirstName,LastName) VALUES ('SF9', '9ers',null)
Comment
Watch Question
Senior Database Administrator
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE