1
Application (:id, :name)

  has_many :steps

Step (:id, application_id, :status)

サンプルデータ:

  Application       Name
 -------------  ------------
    1              'Peter'
    2              'Paul'
    3              'Mary'

  Step    Application ID    Status
 ------   --------------   -------------
    1           1           'new'
    2           2           'new'
    3           1           'in-review'
    4           2           'in-review'
    5           2           'completed'
    6           3           'new'
    7           3           'in-review'

count(*)アプリケーションの最新のステータスのレポートを取得するにはどうすればよいですか?すなわち

   Status       Count       
 ------------- -------
  'in-review'     2
  'completed'     1
4

1 に答える 1

1

持っている、MAXはmysql5.1で動作していないようです

これは機能しているようです

select status, count(status) as "count"
from (
        select status 
        from (
                select * from steps order by created_at desc
             )
        steps_sorted 
        group by application_id
     )
latest_statuses 
group by status;

これはStep.find_by_sqlで使用できます

于 2012-05-29T10:05:08.400 に答える