Hi, I''m currently working on a time-tracking application. To calculate the time spent on a given project, i have to do something like this: SELECT sum(ts.start-ts.stop) FROM timespans AS ts WHERE ts.project_id = 1 I was wondering how I could get single values like this without having to create a whole AR object (like Project.find_by_sql). Anyone done something like this before? Kind regards, Flurin
I would say
duration = connection.select_one("SELECT sum(start-stop) FROM timespans
WHERE project_id = 1")[''sum(start-stop)'']
does the job
-----Ursprüngliche Nachricht-----
Von: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]Im
Auftrag von F. Egger
Gesendet: Dienstag, 18. Januar 2005 09:42
An: Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Betreff: [Rails] AR: getting simple GROUP BY results
Hi,
I''m currently working on a time-tracking application. To calculate the
time spent on a given project, i have to do something like this:
SELECT sum(ts.start-ts.stop) FROM timespans AS ts WHERE ts.project_id = 1
I was wondering how I could get single values like this without having
to create a whole AR object (like Project.find_by_sql).
Anyone done something like this before?
Kind regards,
Flurin
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Exactly what I was looking for! Thanks. Stefan Kaes wrote:>I would say > >duration = connection.select_one("SELECT sum(start-stop) FROM timespans WHERE project_id = 1")[''sum(start-stop)''] > >does the job > >-----Ursprüngliche Nachricht----- >Von: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]Im Auftrag von F. Egger >Gesendet: Dienstag, 18. Januar 2005 09:42 >An: Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Betreff: [Rails] AR: getting simple GROUP BY results > > >Hi, > >I''m currently working on a time-tracking application. To calculate the >time spent on a given project, i have to do something like this: > >SELECT sum(ts.start-ts.stop) FROM timespans AS ts WHERE ts.project_id = 1 > >I was wondering how I could get single values like this without having >to create a whole AR object (like Project.find_by_sql). > >Anyone done something like this before? > >Kind regards, >Flurin >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >