Plain sql to find


#1

Hi,

I have a query like

SELECT call_queue_id,agent_id,count(*) AS count
FROM completed_calls
GROUP BY call_queue_id,agent_id
ORDER BY call_queue_id,agent_id

on a table with following structure:

±----------------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±----------------±------------±-----±----±--------±---------------+
| id | int(11) | | PRI | NULL | auto_increment |
| call_queue_id | int(11) | YES | | NULL | |
| agent_id | int(11) | | | 0 | |
| start_time | datetime | YES | | NULL | |
| end_time | datetime | YES | | NULL | |
| status | char(1) | YES | | c | |
±----------------±------------±-----±----±--------±---------------+

How can I express this query using find?

Right now I am using find_by_sql, but I would like to use find because
I want to use with_scope (I am assuming that with_scope will not work
with find_by_sql)

regards,

raj


#2

Try:

YourModelClass.find(:all, :select => “call_queue_id,agent_id,count(*)
AS count”, :group => “call_queue_id,agent_id”, :order =>
“call_queue_id,agent_id”)

-Paul


#3

On 12/28/06, Paul C. removed_email_address@domain.invalid wrote:

YourModelClass.find(:all, :select => “call_queue_id,agent_id,count(*)
AS count”, :group => “call_queue_id,agent_id”, :order =>
“call_queue_id,agent_id”)

Thanks!! That does work, but when I tried to use :include => this as
well as :group stopped working. Is there a way to get this working
with another table included in?

raj


#4

Unfortunately that won’t work. Rails doesn’t support :group when an
:include is specified.

-Paul