Skip to content

Instantly share code, notes, and snippets.

@phocke
Created April 9, 2015 10:17
Show Gist options
  • Save phocke/581f6a307c916077d86f to your computer and use it in GitHub Desktop.
Save phocke/581f6a307c916077d86f 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

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