How to split table in different range and select 1000 random rows from each split table and join in one table in sql

Viewed 23

I want to split table in bins and then select random 1000 rows from each splitted table and join these rows to final table.

in pyspark i can do like this

df1 = (df.where((df.A >1)&(df.A <10)))
df1=df1.orderBy(F.rand()).limit(1000)
df2 = (df.where((df.A >10)&(df.A <20)))
df2=df2.orderBy(F.rand()).limit(1000)

dfs = [df1, df2]
df_complete = reduce(DataFrame.unionAll, dfs)

how to do this in sql ?

1 Answers

Mysql syntax, assuming new_t has the same structure:

insert into new_t 
select * from t where {condition 1} order by rand() limit 1000
union
select * from t where {condition 2} order by rand() limit 1000
Related