views:

42

answers:

2

I am currently trying to create graph statistics for jobs in a PBS. I have a jobs model that has many fields, among them is a "Group" field. I would like to know how many jobs each group has executed. For that, I need the following query:

SELECT 
jobs.`group`,
COUNT(`group`) AS 'number_of_jobs'
FROM jobs
GROUP BY jobs.`group`

Which returns 2 columns, the group name and how many jobs that group has executed, whoever, I am unable to do so in Ruby on rails. Any help would be appreciated.

A: 

Assuming that you have model named Job for jobs tables

Job.find(
          :all,
          :select => 'group, COUNT(group) AS number_of_jobs',
          :group  => 'group'
      )
Dinesh Atoliya
For some reason this code gave me an SQL syntax error...it produced the following query:SELECT group, COUNT(group) AS number_of_jobs FROM `jobs` GROUP BY group.However I tweaked it a bit, an this worked: Job.all({ :select => "jobs.group AS name, COUNT('group') AS contador", :group =>"jobs.group"})But that doesnt seem to be the "rails way". Thanks!
jalagrange
+1  A: 

group is keyword for mysql so use (`) backtics for it

Job.find(
          :all,
          :select => '`group`, COUNT(`group`) AS number_of_jobs',
          :group  => '`group`'
      )

No check though

Salil
Thank you! this solved it!
jalagrange