sql - 按值选择行并计算回转半径
问题描述
我是 sql/presto 的新手,我正在尝试从这样的表开始计算所有用户 ID的回转半径:
Table
+----------+---------+-------+
| userID | X | Y |
+----------+---------+-------+
| asdasd | 23 | 45 |
| sfasdf | 11 | 12 |
| dfadsg | 93 | 34 |
| asdasd | 17 | 21 |
| dfasdf | 12 | 43 |
| ... | .. | ... |
+----------+---------+-------+
我想返回一个包含所有用户 ID 及其回转半径的表格。
我能够恢复计算 100 点随机样本的回转半径(rog) 的代码,但我很难将它应用到我的桌子上。
with n as (select 100 as n)
--generate a set of points using random XY
, xy as (select SEQUENCE(1,n) as gid, st_point(random(), random()) as geom_point from n)
--distance between each point and centroid
, d as (select gid, st_distance(geom_point, geom_centroid) as dist from xy, c)
--standard deviation of distance
, d_sd as (select stddev(dist) from d)
--square the distance
, d2 as (select gid, dist, dist^2 as dist_squared from d)
--sum the squares and divide by n
, d2sum as (select sum(dist_squared) as dist_squared_sum from d2)
--divide by n
, d2sum_normalize as (select dist_squared_sum / n as dist_norm from d2sum, n)
--square root
, rog as (select sqrt(dist_norm) as radius_of_gyration from d2sum_normalize)
--report radius of gyration and stddev(distance)
select * from rog, d_sd
谢谢您的帮助!
解决方案
推荐阅读
- c# - Active Directory:是否可以将项目添加到 SearchResultCollection
- sql - 我如何使用子查询来获取这里的总价?
- django - 我的登录视图一直说“使用此电子邮件的用户已经存在”。
- phpmailer - PHPMailer:我如何在纯文本版本中保留链接?
- .net - Assembly.Load 在测试用例中失败(强命名验证也失败)......为什么?
- swift - 如何在 Swift 中将 http 转换为 https URL?
- swift - 视觉格式语言并因特征而异
- java - 实现类的实例
- mongodb - 将本地 Meteor MongoDB 导入到 DigitalOcean Ubuntu 服务器
- reactjs - 浏览器窗口中的电子高度未传播