1

使用 AREL / Rails计算我正在尝试执行以下操作:

SELECT to_char(timestamp, 'YYYY-MM-DD') AS segment, COUNT(*) AS counter 
FROM pages
GROUP BY segment
ORDER BY segment

我可以运行类似的东西:

Page.order(FIELD).count(group: FIELD) 
{ a: 1, b: 4, c: 1 }

但是,我无法跨计算字段进行此操作。有什么想法吗?

4

1 回答 1

0

想出了这个:

Page.count(:all, from: "(SELECT to_char(#{SEGMENT}, 'YYYY-MM-DD')  AS segment FROM pages) AS pages", group: "segment", order: "segment")
> SELECT COUNT(*) AS count_all, segment AS segment FROM (SELECT to_char(created_at, 'YYYY-MM-DD') AS segment FROM pages) AS pages GROUP BY segment ORDER BY segment
于 2013-02-15T22:43:08.753 回答