簡體   English   中英

最后一步將SQL查詢轉換為Rails Active Record查詢

[英]Convert an SQL query to Rails Active Record query last step

我有一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM