【发布时间】:2018-11-16 16:40:40
【问题描述】:
我正在构建一个使用 Python(Flask) 和 Redshift 作为数据源备份的分析页面。数据以起始模式呈现,所以我想做的只是在指定时间范围内进行基本聚合和过滤(听起来不像火箭科学)。虽然我找不到任何优雅的方式来做到这一点。
假设我有一个 SQL 查询,可以很好地提供当月的每日统计信息。
with current_month as (
select date
from date_d
where month_name = 'November' AND year_actual = '2018'
order by date
),
filtered as (
select date ,fact.id, fact.created_id,
from fact
join date_d ON date_d.id = fact.created_id
where date_d.month_name = 'November' AND date_d.year_actual = '2018' AND fact.foo = 'bar'
),
total as (
SELECT COUNT(id),DATE(date)
from filtered GROUP BY 2),
SELECT current_month.date, COALESCE(total.count,0) as total
from current_month
LEFT JOIN total ON current_month.date = total.date
group by 1,
order by current_month.date
有什么方法可以执行这个查询并将结果加载到一些预定义的数据结构中?我查看了 SQLAlchemy,因为我不想执行原始 SQL 查询,但 ORM 在这里看起来没用。 看起来唯一的方法是执行原始 SQL 并将其加载到某种模式中(使用棉花糖)。 我查看了this article,它很接近但没有那么详细。
也许我错过了什么?人们应该经常做这样的事情。 或者也许我的方法是错误的?
附:使用 Flask-Restless 进行简单的过滤非常适合我的架构
【问题讨论】:
标签: python sql sqlalchemy data-analysis star-schema