Skip to content

Instantly share code, notes, and snippets.

@phocke
Created April 9, 2015 10:16
Show Gist options
  • Save phocke/5a9467493cac45d7a1b3 to your computer and use it in GitHub Desktop.
Save phocke/5a9467493cac45d7a1b3 to your computer and use it in GitHub Desktop.
Reports sql
## Ruby
@crawl.v2_reports.select { |r| !r.reported_at.nil? }
sort_by { |r| r.reported_at.to_i }.
reverse.
uniq { |r| r.v2_report_template_id }
## Sql
[Stack overflow answer](http://stackoverflow.com/a/7250594)
SELECT t.*
FROM
(SELECT reported_at, MAX(id) as id FROM v2_reports GROUP BY reported_at) a
INNER JOIN v2_reports t ON (t.id = a.id)
## Sequel?
@crawl.v2_reports_dataset
.exclude(reported_at: nil)
.order("reported_at ASC")
.group(:v2_report_template_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment