所以我需要生成一些仪表板,大多数这些仪表板依赖MySQL中的求和查询来工作。
这是模型中的rails查询:
def self.search_three(dyear)
find_by_sql("SELECT quantity, model,
SUM(IF(status = 'Completed',quantity,0)) AS Comp,
SUM(IF(status = 'Shipped',quantity,0)) AS Ship,
SUM(IF(status = 'Cart Submitted',quantity,0)) AS CS,
SUM(quantity) AS total
FROM orders
WHERE YEAR(needby) = '#{dyear}'
GROUP BY model")
end
它可以工作,但问题是rails控制台中的输出不是我期望进入仪表板的。
[#,#,#]如果我将rabar(来自其他地方的输入)指定给2015,则输出。
但是,当我这样做时,在MySQL工作台中:
SELECT quantity,
SUM(IF(status = 'Completed',quantity,0)) AS Comp,
SUM(IF(status = 'Shipped',quantity,0)) AS Ship,
SUM(IF(status = 'Cart Submitted',quantity,0)) AS CS,
SUM(quantity) AS total
FROM orders
WHERE YEAR(needby) = '2015'
GROUP BY model
我得到了
{| !数量|| !Comp ||!发货|| !CS || !总 | - | 40 || 8810 || 0 || 1081 || 19181 ||连接器 | - | 118 || 318 || 0 || 0 || 955 ||欧盟绳索 | - | 256 || 548 || 0 || 0 || 3944 ||美国绳索 |}
我需要显示的图表是总列和图表显示的是上述错误数量。
如何让rails查询使用输出的AS总数而不仅仅是第一列?
答案 0 :(得分:0)
好的,我非常接近;
def self.search_three(dyear)
find_by_sql("SELECT model,
SUM(IF(status = 'Completed',quantity,0)) AS Comp,
SUM(IF(status = 'Shipped',quantity,0)) AS Ship,
SUM(IF(status = 'Cart Submitted',quantity,0)) AS CS,
SUM(quantity) AS quantity
FROM orders
WHERE YEAR(needby) = '#{dyear}'
GROUP BY model")
end
这主要是给了我预期的输出;虽然它确实显示以下内容:
[#<Order id: nil, quantity: 19181, model: "Connector">, #<Order id: nil, quantity: 955, model: "EU Cords">, #<Order id: nil, quantity: 3944, model: "US Cords">]
我认为订单ID部分可能存在于chartkick中,但这可能是后来的另一篇文章。