I need to generate multiple rows for each record in table into another table. This is to generate an excel file for user to key in some data and later import into database.
State | Branch |
---|---|
State1 | Branch1 |
State1 | Branch2 |
State1 | Branch3 |
State2 | Branch1 |
State3 | Branch1 |
State3 | Branch2 |
State4 | Branch1 |
State4 | Branch2 |
State4 | Branch3 |
State4 | Branch4 |
Table above will become like this
State | Branch |
---|---|
State1 | Branch1 |
State1 | Branch2 |
State1 | Branch3 |
State1 | Branch4 |
State1 | Branch5 |
State2 | Branch1 |
State2 | Branch2 |
State2 | Branch3 |
State2 | Branch4 |
State2 | Branch5 |
State3 | Branch1 |
State3 | Branch2 |
State3 | Branch3 |
State3 | Branch4 |
State3 | Branch5 |
State4 | Branch1 |
State4 | Branch2 |
State4 | Branch3 |
State4 | Branch4 |
State4 | Branch5 |
Can see that, State1-Branch1 only have 3 records, but I need to generate 5 rows, and same for other branches.
This will then export to excel file and let user to key in the information.
And the end the file will import back to db.
May I know how to generate the table with 5 lines for each branch?
2
Answers
Here is a way to do it using
GENERATE_SERIES()
andCROSS JOIN
:Demo here
If you only need to generate the
state
labels andbranch
es are listed somewhere:If you already have tables listing all
state
s as well as allbranch
es you want:fiddle