SELECT * FROM checkins WHERE serial=${serial} AND created_at BETWEEN ${startTimestamp} AND ${endTimestamp} ORDER BY created_at DESC liMIT ${limit} OFFSET ${offset}
关于使用Postgres的好策略的任何想法?
解决方法 假设你有PG 9.4这应该做的伎俩:SELECT *FROM checkinsJOIN ( -- The below returns 366 created_at values within the two time points,inclusive SELECT precentile_disc(fraction/365.) WITHIN GROUP (ORDER BY created_at) FROM checkins,generate_serIEs(0,365) f(fraction) WHERE serial = ${serial} AND created_at BETWEEN ${startTimestamp} AND ${endTimestamp}) USING (created_at)ORDER BY created_at DESC;
percentile_disc()
function根据指定的分数从排序组中为您提供离散值,其值最接近提供的分数.与generate_series()
结合使用时,您可以在分数[0.,0.004,0.008,…,1.]处获得一系列此类值.然后将这些值(created_at值,而不是分数)加回到checkins表中以获得最终结果.
对于旧版本的PG,您可以“手动”执行此 *** 作:
SELECT *FROM ( SELECT *,rank() OVER (ORDER BY created_at) AS rnk FROM checkins WHERE serial = ${serial} AND created_at BETWEEN ${startTimestamp} AND ${endTimestamp}) subWHERE rnk % extract(day from ${endTimestamp} - ${startTimestamp}) = 1ORDER BY created_at;
这为startTimestamp和endTimestamp之间的每一天提供了一行,所以如果它们相隔一年就会得到365行.
总结以上是内存溢出为你收集整理的甚至用PostgreSQL进行数据采样全部内容,希望文章能够帮你解决甚至用PostgreSQL进行数据采样所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)