1

Trying to find a simple way to insert some repeating values in two columns in my table, something similar to the rep function in R-

for instance, I need to insert two values (chocolate and vanilla, 4 times each) and I need to insert 4 types of values that repeat twice such as --

flavor_type schedule_type
chocolate   weekly
chocolate   monthly
chocolate   quarterly
chocolate   yearly
vanilla     weekly
vanilla     monthly
vanilla     quarterly
vanilla     yearly

1 Answer 1

4

You can use cross join:

select *
from (values('chocolate'), ('vanilla')) flavor(flavor_type)
cross join (values('weekly'), ('monthly'), ('quarterly'), ('yearly')) schedule(schedule_type)

Output:

flavor_type schedule_type
----------- -------------
chocolate   weekly
chocolate   monthly
chocolate   quarterly
chocolate   yearly
vanilla     weekly
vanilla     monthly
vanilla     quarterly
vanilla     yearly
Sign up to request clarification or add additional context in comments.

2 Comments

Hm, this seems to give me a table with a total of 5 columns and 2 rows: * one column for flavor type with no repetition - just two distinct values * four columns for each respective schedule type, repeated for flavor
@S31, this query produces a resultset with two columns and eight rows, pls see answer.

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.