1

I currently have a table where column 1 is a unique identifier for the remaining columns and I need to insert a new value into the table for each unique identifier. The example is as follows:

Column 1   Column 2   Column 3 
10         Address    True 
10         City       False
10         State      True
20         Address    True
20         City       True
20         State      True

I need to insert a new row based upon each unique identifier in Column 1, like so:

Column 1   Column 2   Column 3
10         Address    True
10         City       False
10         State      True
*10         NEW        NEW*
20         Address    True
20         City       True
20         State      True
*20         NEW        NEW*

For some reason, the SQL script for the quick lookup and insert is just escaping me on a Monday morning. Any help would be appreciated.

Thanks!

1
  • Column 1 is not unique here. Commented Mar 18, 2013 at 15:58

2 Answers 2

1
insert into table-name
(Column1, Column2, Column3)
select Column1, 'NEW', 'NEW*' from table-name group by Column1

You can alternatively use a distinct in the sub-select, but I have started switching to group by, which can be more flexible if I want to change a query to count or something.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @Marlin Pierce, Aarolama Bluenk and AshReva. I'm trying to use a hybrid of the suggestions. My apologies for the use of 'unique' in this case. I would consider myself an average SQL user at best so still learning. At this time, I've tried INSERT INTO table-name VALUES (X, Y, Z) WHERE Column 1 = Random Value. The issue with that code obviously is that I would have to do that for each value of Column 1 (the thing I'm trying to avoid). I'm hoping Marlin's suggestion will do the quick lookup to resolve that.
Thank you again. So I think the thing I'm missing is the quick look up to insert the row multiple times into the table based on each new value in Column 1. I believe I have over 1,000 "unique" values in Column 1 that need to have this additional row added to it. Any thoughts?
Does the "select Column1, 'NEW', 'NEW*' from table-name group by Column1" not give you exactly what you want to insert? If so, my answer should work.
Hi @Marlin Pierce. Worked flawlessly. Thank you!
1

You can try at variant of Marlin Pierce's suggestion of:

INSERT INTO [table-name]
([Column 1], [Column 2], [Column 3])
SELECT [Column 1], 'NEW', 'NEW*' from [table-name] where [Column 2] = 'Address' group by [Column 1];

This creates a single new row for each of your existing rows.

See the SqlFiddle for executable sample.

1 Comment

Thank you again. So I think the thing I'm missing is the quick look up to insert the row multiple times into the table based on each new value in Column 1. I believe I have over 1,000 "unique" values in Column 1 that need to have this additional row added to it. Any thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.