简体   繁体   English

Postgres 查询适用于 Postgres 客户端,但不适用于 Rails Active Record

[英]Postgres query works on Postgres client but not on Rails Active Record

I have this query that works fine on my Postgres client, however when I try on Rails I get an error.我的这个查询在我的 Postgres 客户端上运行良好,但是当我尝试使用 Rails 时出现错误。 Here's the query:这是查询:

sql_query = <<-SQL.squish
  SELECT *
       , total_price - taxes - shipping - total_discount AS net_sales
  FROM (SELECT created_at
             , COALESCE(total_orders, 0)        AS total_orders
             , COALESCE(total_price, 0)         AS total_price
             , COALESCE(taxes, 0)               AS taxes
             , COALESCE(shipping, 0)            AS shipping
             , COALESCE(average_order_value, 0) AS average_order_value
             , COALESCE(total_discount, 0)      AS total_discount
        FROM generate_series(timestamp '2022-07-20'
                 , timestamp '2022-07-26'
                 , interval '1 day') AS g(created_at)
                 LEFT JOIN ( -- ③
            SELECT created_at::date
                 , count(*)            AS total_orders
                 , sum(total_price)    AS total_price
                 , sum(taxes)          AS taxes
                 , sum(shipping)       AS shipping
                 , avg(total_price)    AS average_order_value
                 , sum(total_discount) AS total_discount
            FROM orders
            WHERE shop_id = 43
              AND active                    
              AND created_at >= '2022-07-20'
              AND created_at < '2022-07-27'
            GROUP BY 1) o USING (created_at)
       ) sub
  ORDER BY created_at DESC;
SQL

ActiveRecord::Base.connection.execute(sql_query).values

And this is the error I'm getting:这是我得到的错误:

PG::SyntaxError: ERROR: syntax error at end of input LINE 1: ...P BY 1) o USING (created_at) ) sub ORDER BY created_at DESC; ^

What am I missing here?我在这里想念什么?

The <<-SQL.squish on the start and SQL on the end must be same.开头的<<-SQL.squish和结尾的SQL必须相同。

Change the <<-SQL.squish just to <<-SQL .<<-SQL.squish更改为<<-SQL

Please try this请试试这个

sql_query = <<-SQL SELECT *, total_price - taxes - shipping - total_discount AS net_sales FROM (SELECT created_at, COALESCE(total_orders, 0) AS total_orders, COALESCE(total_price, 0) AS total_price, COALESCE(taxes, 0) AS taxes, COALESCE(shipping, 0) AS shipping, COALESCE(average_order_value, 0) AS average_order_value, COALESCE(total_discount, 0) AS total_discount FROM generate_series(timestamp '2022-07-20', timestamp '2022-07-26', interval '1 day') AS g(created_at) LEFT JOIN ( -- ③ SELECT created_at::date, count(*) AS total_orders, sum(total_price) AS total_price, sum(taxes) AS taxes, sum(shipping) AS shipping, avg(total_price) AS average_order_value, sum(total_discount) AS total_discount FROM orders WHERE shop_id = 43 AND active AND created_at >= '2022-07-20' AND created_at < '2022-07-27' GROUP BY 1) o USING (created_at) ) sub ORDER BY created_at DESC; SQL ActiveRecord::Base.connection.execute(sql_query).values

You can furthur read at: What are these strings called?您可以进一步阅读: 这些字符串叫什么? What is squish? 什么是挤压? Ruby Ruby

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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