使用数据库中的标准差构建图表显示平均值的分布情况
来源:stackoverflow
时间:2024-02-09 22:18:24 299浏览 收藏
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《使用数据库中的标准差构建图表显示平均值的分布情况》,聊聊,希望可以帮助到正在努力赚钱的你。
解释起来很棘手,所以我会将信息缩小到最少:
但首先,我将尝试解释我的最终目标,我想让试用产品的用户确定该产品如何影响一个值(与他们的平均基线相比),然后将所有这些百分比与标准开发人员平均.
我有一个数据库,其中有一个包含 user_id、值和日期的表。
user_id | value | date |
---|---|---|
int | int | int in epoch miliseconds |
然后,我有第二个表,其中指示用户的试用开始和结束的时间以及他们用于所述试用的产品。
user_id | start_date | end_date | product id |
---|---|---|---|
int | int in epoch milisecs | int in epoch milisecs | int |
我想要做的是收集一种产品类型的所有用户试用,并为每个参与的用户获取基线值及其每天的变化百分比。然后对所有这些百分比进行平均,得到每天的标准差。
一个问题是日期需要转换为自 start_date 以来的天数,因此开始日期和前 24 小时之间的任何内容都将集中为第 0 天,接下来的 24 小时将集中为第 1 天,依此类推。所以我会平均每天的百分比
并非每个用户的每一天都会被记录,因此有些用户可能会有多个缺失的日子,所以我不需要将每一天标记为从开始算起的天数
开始日期在用户之间是随机的
所以图表将如下所示:
图片
我更愿意尽可能多地用 sql 来做,但其余的将用 golang 来做。
我就想着抓住每一次的尝试,然后每次的尝试都会有一系列的结果。因此,然后我迭代每个试验,并迭代每个试验的结果,选择第 0 天、第 1 天、第 2 天,并将它们保存在自己的数组中,然后我将对其进行平均。但一切都开始变得超级混乱
例如半伪代码:
db.query("select user_id, start_date from trials where product_id = $1", productid).scan(&trial.userid, &trial.startdate) //extract trials from rows for _, trial := range trials { // extract leadingavgstart from startdate db.queryrow("select avg(value) from results where user_id = $1 date between $2 and $3", trial.userid, leadingavgstart, trial.startdate) // now we have the baseline for the user rows := db.query("select value, date from results where product_id = $1", start) //now we extract the results and have and array //convert dates to dates from start date //...? it just start getting ugly and i believe there has to be a better way }
如何使用 sql 完成大部分繁重的工作?
create table users (id int primary key, name text); create table products (id int primary key, name text); create table values ( id int primary key , user_id int references users(id) , value int , date numeric ); create table trials ( id int primary key , user_id int references users(id) , start_date numeric , end_date numeric , product_id int references products(id) );
insert into users (id, name ) values (1,'john'), (2,'jane'), (3,'billy'), (4,'miranda'); insert into products (id, name ) values (1, 'pill a'), (2, 'pill b'), (3, 'pill c'), (4, 'exercise bal'), (5, 'diet plan'); insert into trials (id,user_id,start_date,end_date,product_id) values (1, 1, 1667896408000, 1668099442000, 1), (2, 1, 1667896408000, 1668099442000, 2), (3, 2, 1667576960000, 1668074401000, 3), (4, 3, 1667896408000, 1668099442000, 1); insert into values (id, user_id, value, date) values (38, 1, 7, 1668182428000), (1, 1, 7, 1668099442000), (2, 1, 8, 1668074401000), (3, 1, 8, 1668012300000), (4, 1, 6, 1668011197000), (5, 1, 6, 1667978268000), (6, 1, 9, 1667925002000), (7, 1, 9, 1667896408000), (8, 1, 4, 1667838601000), (9, 1, 6, 1667803049000), (10, 1, 7, 1667576960000), (12, 1, 5, 1667546428000), (13, 1, 8, 1667490149000), (14, 2, 8, 1668182428000), (15, 2, 7, 1668099442000), (16, 2, 8, 1668074401000), (17, 2, 9, 1668012300000), (18, 2, 6, 1668011197000), (19, 2, 6, 1667978268000), (20, 2, 5, 1667925002000), (21, 2, 9, 1667896408000), (22, 2, 4, 1667803049000), (23, 2, 4, 1667576960000), (24, 2, 5, 1667546428000), (25, 2, 9, 1667490149000), (26, 3, 6, 1668182428000), (27, 3, 7, 1668099442000), (28, 3, 8, 1668074401000), (29, 3, 9, 1668011197000), (30, 3, 6, 1667978268000), (31, 3, 9, 1667925002000), (32, 3, 9, 1667896408000), (33, 3, 8, 1667838601000), (34, 3, 6, 1667803049000), (35, 3, 4, 1667576960000), (36, 3, 5, 1667546428000), (37, 3, 6, 1667490149000);
正确答案
好吧,我明白了,基本上我做了两个内部联接查询,并将它们视为表,然后内部联接它们,并使用分组来求平均值
select query1.product_uuid, query1.days, AVG(query1.value / query2.avg) as avg_percent from ( select DATE_PART( 'day', to_timestamp( values .date / 1000 ):: date - trials.start_date ) as days, trials.uuid as trial_uuid, trials.product_uuid, values .value as value from values as values inner join product_trials as trials ON values .user_id = trials.user_id where values .source = 'Trued' and values .use = 'true' AND trials.start_date IS NOT NULL AND trials.end_date IS NOT NULL AND to_timestamp( values .date / 1000 ):: date > trials.start_date AND to_timestamp( values .date / 1000 ):: date < trials.end_date ) as query1 inner join ( select values .user_id, trials.uuid as trial_uuid, AVG(value) from values inner join product_trials as trials ON values .user_id = trials.user_id where source = 'Trued' and use = true AND trials.start_date IS NOT NULL AND trials.end_date IS NOT NULL AND to_timestamp( values .date / 1000 ):: date < trials.start_date AND DATE_PART( 'day', to_timestamp( values .date / 1000 ):: date - trials.start_date ) > -20 GROUP BY values .user_id, trials.uuid ) as query2 ON query1.trial_uuid = query2.trial_uuid where query2.avg > 0 GROUP BY query1.days, query1.product_uuid ORDER BY query1.product_uuid, query1.days
今天关于《使用数据库中的标准差构建图表显示平均值的分布情况》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习