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_series
is 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_series
is 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 sequentialid
column and never delete rows from it, you can just select theid
column 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!

本文討論了使用MySQL的Alter Table語句修改表,包括添加/刪除列,重命名表/列以及更改列數據類型。

文章討論了為MySQL配置SSL/TLS加密,包括證書生成和驗證。主要問題是使用自簽名證書的安全含義。[角色計數:159]

文章討論了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比較了它們對初學者和高級用戶的功能和適合性。[159個字符]

本文討論了使用Drop Table語句在MySQL中放下表,並強調了預防措施和風險。它強調,沒有備份,該動作是不可逆轉的,詳細介紹了恢復方法和潛在的生產環境危害。

本文討論了在PostgreSQL,MySQL和MongoDB等各個數據庫中的JSON列上創建索引,以增強查詢性能。它解釋了索引特定的JSON路徑的語法和好處,並列出了支持的數據庫系統。

文章討論了使用準備好的語句,輸入驗證和強密碼策略確保針對SQL注入和蠻力攻擊的MySQL。(159個字符)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。