最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

GenerateSeriesinRedshiftandMySQL_MySQL

来源:动视网 责编:小采 时间:2020-11-09 19:18:22
文档

GenerateSeriesinRedshiftandMySQL_MySQL

GenerateSeriesinRedshiftandMySQL_MySQL: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 t
推荐度:
导读GenerateSeriesinRedshiftandMySQL_MySQL: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 t
 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:

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:

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!

文档

GenerateSeriesinRedshiftandMySQL_MySQL

GenerateSeriesinRedshiftandMySQL_MySQL: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 t
推荐度:
标签: in mysql Series
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top