问题描述
如何在Rails 4中实现跟踪?
How to achieve following in rails 4?
这是我的PostgreSQL查询,可以正常工作
Here is my PostgreSQL query and works correctly
SELECT lower(name) as c_name, count(*) as cc FROM categories GROUP BY c_name ORDER BY cc desc LIMIT 3
ii给了我以下结果
"ecommerce solutions";6
"vmware";6
"big data analytics";5
但是当我在Rails 4中编写与
But when I write same query in Rails 4 as
Category.select("lower(name) as c_name, count(*) as cc").group("c_name").order("cc desc").limit(3)
以上查询生成与上面的PostgreSQL查询相同的以下查询
Above query generates following query which is same as above PostgreSQL query
SELECT lower(name) as c_name, count(*) as cc FROM "categories" GROUP BY c_name ORDER BY cc desc LIMIT 3
给出以下结果
[#<Category id: nil>, #<Category id: nil>, #<Category id: nil>]
为什么会这样?当我将查询切到
Why this is happening ? when I cut my query to
Category.select("lower(name)").limit(3)
这也不起作用,结果变成
This also does not work, and results into
[#<Category id: nil>, #<Category id: nil>, #<Category id: nil>]
我想在 lower(name)
上分组,计数为聚合,我有很多<$ 类别
模型
I want to do group on lower(name)
with count as aggregation, I have many variances of name
in Category
model
Ex中的c $ c>名称。数据集
Ex. data set
"Vmware" and "VMware" and "VMWARE" and "vmware"
我如何在Rails 4中对lower(name)进行分组?
How can I group on lower(name) in Rails 4?
用简单的话说,如何在Rails 4中编写以下等效查询?
In simple words, How to write equivalent query of following in Rails 4?
SELECT lower(name) as c_name, count(*) as cc FROM categories GROUP BY c_name ORDER BY cc desc LIMIT 3
推荐答案
结果正确,但Rails显示错误。只需手动访问结果即可:
The result is correct, but Rails displays it wrong. Just access results manually:
results = Category.select("lower(name) as c_name, count(*) as cc").group("c_name").order("cc desc").limit(3)
results.first['c_name']
这篇关于选择带有分组依据的小写字母列,或在Rails 4中选择小写(列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!