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!
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.