我需要调整此查询到rails我不知道如何调整它由于出现错误,这是原始查询:
SELECT Convert(varchar(10),Fecha,103) as Fechax, count(*) NoPedidos
FROM pedidos
WHERE cancelado=0 AND
group by month(Fechax),Convert(varchar(10),Fecha,103)
order by month(Fechax)
这是我到目前为止所做的:
query = select('Convert(varchar(10),Fecha,103) AS Fechax, count(*) NoPedidos')
.where("Cancelado = 0")
如何添加group_by和order_by?
答案 0 :(得分:0)
query = select(' Convert(varchar(10),Fecha,103)AS Fechax,count(*)NoPedidos') .where(" Cancelado = 0")。group(" column_name")。order(" column desc / asc")
答案 1 :(得分:0)
代码如下所示:
query = Pedido.select('Convert(varchar(10),Fecha,103) AS Fechax, count(*) NoPedidos').where("Cancelado = ?", 0).group("month(Fechax), Convert(varchar(10),Fecha,103)").order("month(Fecha)")
您可以在order()方法中指定顺序(ASC / DESC)。我认为它现在适合你。
阅读本文,它在处理ActiveRecord方面给了我很多帮助:http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-group