Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL generate Data based of the ids of three tables

I have three tables store, gender, age_group each of these tables have ids. I need to generate table data for each one all possible combinations of the three.

ex. store_id = (1,2,3) gender_id = (1,2,3) age_group_id = (1,2,3)

so that i have a table that looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

|store_id|gender_id|age_group_id|
|:------:|:-------:|:----------:|
|  1     |     1   |      1     |
|  1     |     2   |      1     |
|  1     |     3   |      1     |
|  2     |     1   |      3     |
|  2     |     2   |      3     |
|  3     |     1   |      3     |
|  3     |     2   |      3     |


etc. continuing on until each combination is populated, any suggestions on best approach to do this in SQL

>Solution :

Cross join the three tables:

select
  s.Id as store_id,
  g.Id as gender_id,
  a.Id as age_group_id
from store s 
cross join gender g 
cross join age_group a
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading