简体   繁体   中英

Convert an SQL query to Rails Active Record query last step

I have an SQL query like the following:

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

I have been able to convert the middle part of the query to Rails query as follows:

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

The only part where I am stuck is the first line of SQL query, ie

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

and the last

) y

Please help me solve this problem. Thanks.

Use the select function along with a from to wrap this query in an outer SELECT / FROM clause:

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

Note that the :y symbol in the second argument specifies the alias of the subquery ( y in your case).

Also, while I can't check this against your code directly, you may find the following formatting more readable:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM