首頁 > 資料庫 > mysql教程 > Generate Series in Redshift and MySQL_MySQL

Generate Series in Redshift and MySQL_MySQL

WBOY
發布: 2016-06-01 13:13:47
原創
1028 人瀏覽過

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:

<code>select now()::date - generate_series(0, 59)</code>
登入後複製

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).

<code>select n from numbers;</code>
登入後複製

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

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

Redshift:

<code>select (getdate()::date - n)::date from numbers</code>
登入後複製

MySQL:

<code>select date_sub(date(now()), interval n day) from numbers</code>
登入後複製

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:

<code>select row_number() over (order by true) as nfrom users limit 60</code>
登入後複製

And now creating the list of dates directly:

<code>select (getdate()::date - row_number() over (order by true))::date as nfrom users limit 60</code>
登入後複製

MySQL: Date Series using Variables

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

<code>set @n:=-1;select (select @n:= @n+1) nfrom users limit 60</code>
登入後複製

And now creating the list of dates directly:

<code>set @n:=date(now() + interval 1 day);select (select @n:= @n - interval 1 day) nfrom users limit 60</code>
登入後複製

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

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板