I want to duplicate a specific teacher so that they appear twice in the output of a SELECT
statement.
Teacher | Grade | District |
---|---|---|
Mr. Smith | 3rd | West |
Mrs. John | 4th | South |
Mr. Cameron | 2nd | North |
Mr. Cameron | 2nd | North |
Kirk Horn | 1st | West |
Desired result, after duplicating ‘Mr. Cameron’:
Teacher | Grade | District |
---|---|---|
Mr. Smith | 3rd | West |
Mrs. John | 4th | South |
Mr. Cameron | 2nd | North |
Mr. Cameron | 2nd | North |
Mr. Cameron | 2nd | North |
Mr. Cameron | 2nd | North |
Kirk Horn | 1st | West |
What would a SELECT
statement look like – without creating a new a table?
I want to do something like this but without the INSERT
:
https://dba.stackexchange.com/questions/142414/easiest-way-to-duplicate-rows
2
Answers
You could use
UNION ALL
https://dbfiddle.uk/rBpSL8NW
If you want to force/predict how many duplicate values for
Mr. Cameron
you will add ,try below query which add only one duplicate valuelimit 1
https://dbfiddle.uk/uUX5QD3F
If the table is big, and the filter is not selective and backed by an index, then this "trick" avoids a second sequential scan over the table – using PostgreSQL:
fiddle
It’s short syntax for a
LATERAL
join. See:fiddle
If you need more copies, just replace ‘2’ above.
Postgres has a hard time estimating the number of rows to expect with this construct, which may confuse query planning. Stu’s variant (doing the same) is slightly more expensive, but easier to estimate for query planning. Syntax needs to be adapted for Postgres:
fiddle