本文介绍了Postgresql:生成序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的查询生成一行 DNA 序列
The query below generates a line of DNA sequence
prepare dna_length(int) as
with t1 as (select chr(65) as s union select chr(67) union select chr(71) union select chr(84) ),
t2 as ( select s, row_number() over() as rn from t1),
t3 as ( select generate_series(1,$1) as i,round(random() * 4 + 0.5) as rn ),
t4 as ( select t2.s from t2 join t3 on (t2.rn=t3.rn))
select array_to_string(array(select s from t4),'') as dna ;
execute dna_length(20);
结果 - dnaCCCGGTTCTCCCCATGCAGG
Result - dnaCCCGGTTCTCCCCATGCAGG
我想生成 10 个随机 DNA 序列
I will like to generate 10 random DNA SEQUENCES
推荐答案
类似的事情?
select x, string_agg((array['A', 'C', 'G', 'T'])[1 + floor(random() * 4)], '')
from generate_series(1, 20, 1) gsn(n) cross join
generate_series(1, 10, 1) gsx(x)
group by x
这篇关于Postgresql:生成序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!