我有两张桌子.
表1包含公司,其位置在名为的列中使用lat/lng坐标进行地理配准the_geom
表2还包含来自Table1的相同公司,而不是地理参考,以及其他数百个地址为地理参考的公司.
我需要做的就是将Table1公司的the_geomlat/lng值插入表2中的相应条目.这些插入可以基于的共同点是列.address
简单的问题,我确定,但我很少使用SQL.
您好我有两个表(表1和表2下文),并希望基于最接近时间戳形成expected_output加入他们的行列.如果可能的话,涉及dplyr的某种解决方案会很好,但如果它进一步使事情变得复杂则不会.
table1 =
structure(list(date = structure(c(1437051300, 1434773700, 1431457200
), class = c("POSIXct", "POSIXt"), tzone = ""), val1 = c(94L,
33L, 53L)), .Names = c("date", "val1"), row.names = c(NA, -3L
), class = "data.frame")
table2 =
structure(list(date = structure(c(1430248288, 1435690482, 1434050843
), class = c("POSIXct", "POSIXt"), tzone = ""), val2 = c(67L,
90L, 18L)), .Names = c("date", "val2"), row.names = c(NA, -3L
), class = "data.frame")
expected_output =
structure(list(date = structure(c(1437051300, 1434773700, 1431457200
), class = c("POSIXct", "POSIXt"), tzone = ""), …Run Code Online (Sandbox Code Playgroud) 我注意到运行这个子查询
SELECT ST_Area(ST_Union(ST_Transform(ST_Intersection((SELECT poly1.the_geom from poly1 WHERE poly1.polygon_type ='P'),poly2.the_geom),3857)))
AS area_of_P FROM poly1,poly2
比运行此连接要慢得多
SELECT ST_AREA(ST_Union(ST_Transform(ST_Intersection(poly1.the_geom,poly2.the_geom),3857)))
AS area_of_poly
来自poly2
在st_intersects上左边连接poly1(poly1.the_geom,poly2.the_geom)
在哪里poly2.polygon_type ='P'
但是,我需要扩展这个第二个连接版本以返回更多列,每个列都计算出给定多边形类型的区域,即
SELECT ST_Area(ST_Union(ST_Transform(ST_Intersection((SELECT poly1.the_geom from poly1 WHERE poly1.polygon_type ='P'),poly2.the_geom),3857)))AS area_of_P,
ST_Area(ST_Union(ST_Transform(ST_Intersection((SELECT poly1.the_geom from poly1 WHERE poly1.polygon_type ='S'),poly2.the_geom),3857)))AS area_of_S
来自poly1,poly2
我正在寻找一种解决方案来添加"desired_result"列,最好使用dplyr和/或ave().请参阅此处的数据框,其中组是"section",我希望我的"desired_results"列按顺序计数的唯一实例位于"exhibit"中:
structure(list(section = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), exhibit = structure(c(1L,
2L, 3L, 3L, 1L, 2L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor"),
desired_result = c(1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L)), .Names = c("section",
"exhibit", "desired_result"), class = "data.frame", row.names = c(NA,
-8L))
Run Code Online (Sandbox Code Playgroud) 在下面的数据框中,我想过滤包含人员“a”、“b”和“c”的组:
df <- structure(list(group = c(1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4),
person = structure(c(1L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 1L,
2L, 3L, 4L), .Label = c("a", "b", "c", "e"), class = "factor")), .Names =
c("group",
"person"), row.names = c(NA, -12L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud) dplyr ×3
r ×3
dataframe ×2
postgis ×2
postgresql ×2
sql ×2
optimization ×1
posixct ×1
sql-update ×1
timestamp ×1