QA@IT
«回答へ戻る

回答を投稿

よくある、「GROUP BYに書けるのは集約関数と(以下略」ではないのですね。

試してみましたが、こちらではとくに問題なく実行できてしまいました。(再現に使ったアプリを作成するためのテンプレート)

$ rails new mswim -d postgresql --skip-bundle -m https://raw.github.com/gist/3156194/974a0bf55ae74110da25c8d7ac9cac39aee95f64/gistfile1.rb

$ cd mswim
$ bundle exec rake about
About your application's environment
Ruby version              1.9.3 (x86_64-darwin11.4.2)
:
Rails version             3.2.6
:
Environment               development
Database adapter          postgresql
:

$ bundle show
:
  * pg (0.14.0)

$ port installed postgresql91\*     
The following ports are currently installed:
  postgresql91 @9.1.4_0 (active)
  postgresql91-server @9.1.4_0 (active)

$ script/rails dbconsole
mswim_development=> \d swims
                                     Table "public.swims"
   Column   |            Type             |                     Modifiers                      
------------+-----------------------------+----------------------------------------------------
 id         | integer                     | not null default nextval('swims_id_seq'::regclass)
 length     | integer                     | 
 swam_at    | date                        | 
 note       | text                        | 
 created_at | timestamp without time zone | not null
 updated_at | timestamp without time zone | not null
     Indexes:
        "swims_pkey" PRIMARY KEY, btree (id)

$ script/rails console
Loading development environment (Rails 3.2.6)
>> Swim.group("to_char(swam_at, 'yyyymm')").sum(:length)
   (0.1ms)  SHOW max_identifier_length
   (3.0ms)  SELECT SUM("swims"."length") AS sum_length,
        to_char(swam_at, 'yyyymm') AS to_char_swam_at_yyyymm
        FROM "swims" GROUP BY to_char(swam_at, 'yyyymm')
=> {"201201"=>29000, "201207"=>16000, "201206"=>22100, "201204"=>22200, "201203"=>21500, "201205"=>22500, "201202"=>18900}
よくある、「GROUP BYに書けるのは集約関数と(以下略」ではないのですね。

試してみましたが、こちらではとくに問題なく実行できてしまいました。([再現に使ったアプリを作成するためのテンプレート](https://gist.github.com/3156194))

    $ rails new mswim -d postgresql --skip-bundle -m https://raw.github.com/gist/3156194/974a0bf55ae74110da25c8d7ac9cac39aee95f64/gistfile1.rb

    $ cd mswim
    $ bundle exec rake about
    About your application's environment
    Ruby version              1.9.3 (x86_64-darwin11.4.2)
    :
    Rails version             3.2.6
    :
    Environment               development
    Database adapter          postgresql
    :

    $ bundle show
    :
      * pg (0.14.0)

    $ port installed postgresql91\*     
    The following ports are currently installed:
      postgresql91 @9.1.4_0 (active)
      postgresql91-server @9.1.4_0 (active)

    $ script/rails dbconsole
    mswim_development=> \d swims
                                         Table "public.swims"
       Column   |            Type             |                     Modifiers                      
    ------------+-----------------------------+----------------------------------------------------
     id         | integer                     | not null default nextval('swims_id_seq'::regclass)
     length     | integer                     | 
     swam_at    | date                        | 
     note       | text                        | 
     created_at | timestamp without time zone | not null
     updated_at | timestamp without time zone | not null
         Indexes:
            "swims_pkey" PRIMARY KEY, btree (id)

    $ script/rails console
    Loading development environment (Rails 3.2.6)
    >> Swim.group("to_char(swam_at, 'yyyymm')").sum(:length)
       (0.1ms)  SHOW max_identifier_length
       (3.0ms)  SELECT SUM("swims"."length") AS sum_length,
            to_char(swam_at, 'yyyymm') AS to_char_swam_at_yyyymm
            FROM "swims" GROUP BY to_char(swam_at, 'yyyymm')
    => {"201201"=>29000, "201207"=>16000, "201206"=>22100, "201204"=>22200, "201203"=>21500, "201205"=>22500, "201202"=>18900}