Generate Series in Redshift and MySQL_MySQL

php中文网
发布: 2016-06-01 13:13:47
原创
1125人浏览过

a lot of the charts and tables made inperiscopeare time series, and the queries behind them are often easier when you can join and aggregate against a list of dates. not having a complete list of dates causes gaps in the results, changing them in a misleading way:

Generate Series in Redshift and MySQL_MySQL

Postgres has a great function for generating a list of dates (seeUse generate_series to get continuous results), and making a list of the last 60 days withgenerate_seriesis easy:

select now()::date - generate_series(0, 59)
登录后复制

Accomplishing the same thing in Redshift and MySQL requires a little more work.

Date Series from a Numbers Table

The simplest alternative togenerate_seriesis to create a table containing a continuous list of numbers, starting at 0, and select from that table. (If you have a table with a sequentialidcolumn and never delete rows from it, you can just select theidcolumn from that table instead of creating a new numbers table).

select n from numbers;
登录后复制

Returns this list of rows: 0, 1, 2, 3...

Now that you have a numbers table, convert each number into a date:

Redshift:

select (getdate()::date - n)::date from numbers
登录后复制

MySQL:

Stable Video
Stable Video

Stability AI 发布的开源AI视频大模型,用文字或图像创建视频,把你的概念变成迷人的电影

Stable Video 227
查看详情 Stable Video
select date_sub(date(now()), interval n day) from numbers
登录后复制

A numbers table is more convenient than a dates table since it never needs to be refreshed with new dates.

Redshift: Date Series using Window Functions

If you don't have the option to create a numbers table, you can build one on the fly using a window function. All you need is a table that has at least as many rows as the number of dates desired. Using a window function, number the rows in any table to get a list of numbers, and then convert that to a list of dates:

select row_number() over (order by true) as nfrom users limit 60
登录后复制

And now creating the list of dates directly:

select (getdate()::date - row_number() over (order by true))::date as nfrom users limit 60
登录后复制

MySQL: Date Series using Variables

With variables in MySQL, we can generate a numbers table by treating a select statement as a for loop:

set @n:=-1;select (select @n:= @n+1) nfrom users limit 60
登录后复制

And now creating the list of dates directly:

set @n:=date(now() + interval 1 day);select (select @n:= @n - interval 1 day) nfrom users limit 60
登录后复制

Now that we've made a list of dates, aggregating and joining data from other tables for time series charts is a breeze!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号