Sql 根据Athena中两个表的日期获取行的查询
我有两张桌子叫master_tbl和anom_table 详情如下: 硕士学位Sql 根据Athena中两个表的日期获取行的查询,sql,postgresql,presto,Sql,Postgresql,Presto,我有两张桌子叫master_tbl和anom_table 详情如下: 硕士学位 date id country value 2017-01-01 26 US 2 2017-01-02 26 US 4 2017-01-03 26 US 9 2017-01-04
date id country value
2017-01-01 26 US 2
2017-01-02 26 US 4
2017-01-03 26 US 9
2017-01-04 26 US 2
2017-01-05 26 US 4
2017-01-06 26 US 1
2017-01-07 26 US 5
2017-01-08 26 US 3
2017-01-09 26 US 100
2017-01-10 26 US 4
anom_tbl
date id country anoms
2017-01-01 26 US 0
2017-01-02 26 US 0
2017-01-03 26 US 9
2017-01-04 26 US 0
2017-01-05 26 US 0
2017-01-06 26 US 0
2017-01-07 26 US 0
2017-01-08 26 US 0
2017-01-09 26 US 100
2017-01-10 26 US 0
我想从master_tbl创建第三个表,并与anom_tbl连接,以仅选择在from anom_tbl中的anom列中具有值的日期以及master_tbl中该日期的前一天和后一天的行
date id country value
2017-01-01 26 US 2
2017-01-02 26 US 4
2017-01-03 26 US 9
2017-01-04 26 US 2
2017-01-05 26 US 4
2017-01-06 26 US 1
2017-01-07 26 US 5
2017-01-08 26 US 3
2017-01-09 26 US 100
2017-01-10 26 US 4
最后,我想要下表
date id country value
2017-01-02 26 US 2
2017-01-03 26 US 9
2017-01-04 26 US 4
2017-01-08 26 US 3
2017-01-09 26 US 100
2017-01-10 26 US 4
因为我有大数据,所以我花时间在R或python中运行它,然后我想在AWS athena中创建表
我在雅典娜中尝试了以下代码,但是它不起作用
FROM
(SELECT t2.value,
t1.id,
t1.country AS country,
cast(t1.date AS DATE) AS orig_date
FROM
(SELECT id,
country,
date
FROM anom_tbl) t1
JOIN master_tbl t2
ON t2.id=t1.id
AND t2.country= t1.country
AND t2.date=t1.date) t3
JOIN master_tbl t2
ON t3.id=t2.id
AND t3.country=t2.country
where t2.date IN(GETDATE()-1)
能否请您帮助我修改sql代码以获得正确的结果。如果我正确地遵循了您的要求,您可以使用exists执行此操作:
这将使主表中的所有记录在+/-1天的间隔内,其中anom表中存在相同id和国家/地区的另一条记录(非0值)。请解释要实现的逻辑。这并不明显。正如我所解释的,我想从master_tbl获取值,其中anom_tbl中的value列的值不是0,并且在该日期前后的一天。谢谢你能告诉我存在什么吗do@user:我刚刚更新了我的答案,并做了一个简短的解释。它运行良好,但是您需要添加=登录,并且m.date