Skip to content

Instantly share code, notes, and snippets.

@danchoi
Created March 27, 2014 00:52
Show Gist options
  • Select an option

  • Save danchoi/9797466 to your computer and use it in GitHub Desktop.

Select an option

Save danchoi/9797466 to your computer and use it in GitHub Desktop.
sparkline query
set @months := 1;
select
user_id,
group_concat(note_cnt_2 order by date) sparkline
from
(select user_id, date, coalesce(sum(note_cnt), 0) note_cnt_2 from
(select u.user_id, date_series.date, note_cnt from
(select id user_id from users) u cross join
(select selected_date date from (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between (now() - interval @months month) and cast(now() as date)) date_series
left join
(select notes.user_id, cast(created_at as date) date, count(notes.id) note_cnt
from notes
where
deleted_at is null AND
notes.created_at > now() - interval @months month
group by notes.user_id, cast(created_at as date)) n
on (n.user_id = u.user_id and n.date = date_series.date)) t
group by user_id, date order by user_id, date) t2
group by t2.user_id ;
@danchoi

danchoi commented Mar 27, 2014

Copy link
Copy Markdown
Author

Suck it ActiveRecord!

@gilesbowkett

Copy link
Copy Markdown

where is the Like button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment