在SQL中插入重复值

尝试找到一种简单的方法在表的两列中插入一些重复值,类似于R-

中的rep函数

例如,我需要插入两个值(巧克力和香草,各4次),并且需要插入4种重复两次的值,例如--

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

解决方案

可以使用cross join

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

输出:

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

相关文章