Ruby on rails 3.2 Rails 3.2 Chartkick折线图分组查询未正确显示

Ruby on rails 3.2 Rails 3.2 Chartkick折线图分组查询未正确显示,ruby-on-rails-3.2,chartkick,active-record-query,Ruby On Rails 3.2,Chartkick,Active Record Query,我正在尝试使用chartkick来显示一个多系列的折线图,它跟踪 过去一年中每月的进出口材料总流量 查询应生成类似于以下内容的内容: SELECT SUM(ship_total_net_weight) AS sum_ship_total_net_weight, month(ship_date) AS month_ship_date, year(ship_date) AS year_ship_date FROM [Downstream] WHERE i

我正在尝试使用chartkick来显示一个多系列的折线图,它跟踪 过去一年中每月的进出口材料总流量

查询应生成类似于以下内容的内容:

    SELECT SUM(ship_total_net_weight) AS sum_ship_total_net_weight,
    month(ship_date) AS month_ship_date, year(ship_date) 
    AS  year_ship_date 
    FROM [Downstream]
    WHERE is_active = 1 AND from_company_id = 89 
    AND
    (ship_date BETWEEN '2014-06-15 18:15:26.196' 
    AND '2015-06-15 18:15:26.196') 
    AND (to_company_id <> 89) 
    GROUP BY month(ship_date), year(ship_date)
    order By  year(ship_date), month(ship_date)
我的图表井涌代码如下:

<%= line_chart [
              {name: "Inbound", data: Downstream.where(:to_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now).where('from_company_id <> ?', current_user.company_id ).group('month(ship_date), year(ship_date)').sum(:ship_total_net_weight)},
              {name: "Outbound", data: Downstream.where(:from_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now ).where('to_company_id <> ?', current_user.company_id ).group('month(ship_date), year(ship_date)').sum(:ship_total_net_weight)}
               ] %>
我从chart kick中得到的结果是每个系列都有一条线,只有两个数据点,一个在2013年12月31日,一个在2014年12月31日

对数据库运行上述SQL将生成9个结果,范围从2014年6月到2015年2月

为什么数据集不同?我的图表踢语法错误吗?我是否需要以某种方式更改查询


提前谢谢你的帮助

我找到了解决方案,我的查询返回单独的日期/月份列,因此我需要更改group子句,下面的折线图工作正常:

<%= line_chart [
                {name: "Inbound", data: Downstream.where(:to_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now).where('from_company_id <> ?', current_user.company_id ).group('dateadd(month, datediff(month,1,ship_date),1)').sum(:ship_total_net_weight)},
                {name: "Outbound", data: Downstream.where(:from_company_id => current_user.company_id, :ship_date => 1.year.ago..Time.now ).where('to_company_id <> ?', current_user.company_id ).group('dateadd(month, datediff(month,1,ship_date),1)').sum(:ship_total_net_weight)}
                        ] %>
使用group子句中的dateadd和datediff函数,可以按日期或日期时间字段的月/年进行排序,并在单个列中返回完整日期

-旁注-我使用的SQLServer不受groupdate gem的支持,groupdate gem通常与chartkick一起使用