mysql - 最后一步将 SQL 查询转换为 Rails Active Record 查询

标签 mysql ruby-on-rails ruby-on-rails-4 activerecord

我有一个如下所示的 SQL 查询:

select y.bid, format(((sum(y.loyalty_checks)/SUM(y.total_receipts_in_POS)) * 100),2) as Participation_Rate_or_percentage_loyalty_checkins from
(
  SELECT receipt_stats.`business_id` as bid,  
    SUM(receipt_stats.total_receipts) AS total_receipts_in_POS,
    c.loyalty_checks AS loyalty_checks
  FROM receipt_stats 
  left JOIN
 (
   SELECT location_id as l, COUNT(checkins.`created_at`) AS loyalty_checks
   FROM checkins 
   left join locations on locations.id = checkins.`location_id`
   WHERE checkins.business_id = 570 and
   DATE(receipt_date) BETWEEN '2016-01-01' and '2016-01-31'
   AND checkins.STATUS = 'loyalty' and checkins.`approved` = 1 
   GROUP BY checkins.location_id
   )  c ON c.l = receipt_stats.location_id
   where  
   date(receipt_stats.`receipt_date`) between '2016-01-01' and '2016-01-31'
   and receipt_stats.`business_id` = 570
   group by  receipt_stats.`business_id`, receipt_stats.`location_id`
) y

我已经能够将查询的中间部分转换为 Rails 查询,如下所示:

def fetch_participation_rate(start_point,end_point)
  receipt_stat_data = ReceiptStat.select("business_id", "SUM(receipt_stats.total_receipts) AS total_receipts_in_POS")
  checkin = loyalty_checkins.select("location_id", "COUNT(checkins.`created_at`) AS loyalty_checks").where("DATE(receipt_date) BETWEEN ? and ?",start_point, end_point).
  group("checkins.location_id")
  stat = receipt_stat_data.select("c.loyalty_checks AS loyalty_checks").joins("left join (#{checkin.to_sql}) c ON c.location_id = receipt_stats.location_id")
  stat = stat.where("date(receipt_stats.receipt_date) between ? and ?", start_point, end_point)
  stat = stat.where("receipt_stats.business_id = ?", business.id)
  stat = stat.group("receipt_stats.business_id, receipt_stats.location_id")
end

我卡住的唯一部分是 SQL 查询的第一行,即

select y.bid, format(((sum(y.loyalty_checks)/SUM(y.total_receipts_in_POS)) * 100),2) as Participation_Rate_or_percentage_loyalty_checkins from

最后一个

) y

请帮我解决这个问题。 谢谢。

最佳答案

使用 select 函数和 from 将此查询包装在外部 SELECT/FROM 子句中:

def fetch_participation_rate(start_point,end_point)
  receipt_stat_data = ReceiptStat.select("business_id", "SUM(receipt_stats.total_receipts) AS total_receipts_in_POS")
  checkin = loyalty_checkins.select("location_id", "COUNT(checkins.`created_at`) AS loyalty_checks").where("DATE(receipt_date) BETWEEN ? and ?",start_point, end_point).
    group("checkins.location_id")
  stat = receipt_stat_data.select("c.loyalty_checks AS loyalty_checks").joins("left join (#{checkin.to_sql}) c ON c.location_id = receipt_stats.location_id")
  stat = stat.where("date(receipt_stats.receipt_date) between ? and ?", start_point, end_point)
  stat = stat.where("receipt_stats.business_id = ?", business.id)
  stat = stat.group("receipt_stats.business_id, receipt_stats.location_id")

  # Wrap our query in an outer query:
  ReceiptStat.select("y.bid, format(((sum(y.loyalty_checks)/SUM(y.total_receipts_in_POS)) * 100),2) as Participation_Rate_or_percentage_loyalty_checkins").from(stat, :y)
end

请注意,第二个参数中的 :y 符号指定子查询的别名(在您的例子中为 y)。

此外,虽然我无法直接根据您的代码检查这一点,但您可能会发现以下格式更具可读性:

def fetch_participation_rate(start_point, end_point)
  checkin = loyalty_checkins
              .select("location_id", "COUNT(checkins.`created_at`) AS loyalty_checks")
              .where("DATE(receipt_date) BETWEEN ? AND ?", start_point, end_point)
              .group("checkins.location_id")

  stat = ReceiptStat.select("business_id, SUM(receipt_stats.total_receipts) AS total_receipts_in_POS, c.loyalty_checks AS loyalty_checks")
           .joins("LEFT JOIN (#{checkin.to_sql}) c ON c.location_id = receipt_stats.location_id")
           .where("DATE(receipt_stats.receipt_date) BETWEEN ? AND ?", start_point, end_point)
           .where("receipt_stats.business_id = ?", business.id)
           .group("receipt_stats.business_id, receipt_stats.location_id")

  ReceiptStat
    .select("y.bid, FORMAT((SUM(y.loyalty_checks)/SUM(y.total_receipts_in_POS) * 100), 2) AS Participation_Rate_or_percentage_loyalty_checkins")
    .from(stat, :y)
end

关于mysql - 最后一步将 SQL 查询转换为 Rails Active Record 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043136/

相关文章:

PHP-插入或删除数据库表时如何调用函数

php - 是否可以在 mysql 5.6 上运行 drupal 6

javascript - Ruby On Rails 应用程序中 jPlayer 的基本实现

ruby-on-rails - 如何将 html5 数据属性添加到 Rails form_tag

ruby-on-rails - 让 postgresql 在 cloud9 上工作

php - 如何使用 Ajax 和 json 从 mysql 中填充选择

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

ruby-on-rails - 在 GIT 中处理 Rails db/schema.rb 文件的正确方法是什么?

macos - Ruby Racer 惰性符号绑定(bind)失败

ruby-on-rails - 将标志从 View 传递到 Rails 中的布局