如何将其翻译成Rails查询?

huangapple go评论66阅读模式
英文:

how to translate to rails query?

问题

Ruby 2.3.8

Rails 5.0.0.1

我有问题需要翻译到Rails查询。

我有一个查询,用于查找重复数据:

SELECT count(*) FROM (SELECT date, code, count(*) AS cnt FROM customers WHERE date >= '2021-01-01' GROUP BY date, code) p WHERE p.cnt > 1;

我还想要在Rails批处理中使用记录器打印以检查结果,但它在Rails批处理中不起作用。

英文:

Ruby 2.3.8

Rails 5.0.0.1

I have problems translate to rails query.

I have a query which is finding duplicate data:

SELECT count(*) FROM (SELECT date, code, count(*) AS cnt FROM customers WHERE date >= '2021-01-01' GROUP BY date, code) p WHERE p.cnt > 1;

and I also would like to print for checking the result with logger but it doesn't work in rails batch.

答案1

得分: 2

You probably need having if I understand the question correctly:

Customer.where("date >= '2021-01-01'").having('count(*) > 1').group(:date, :code)
英文:

You probably need having if I understand the question correctly:

Customer.where("date >= '2021-01-01'").having('count(*) > 1').group(:date, :code)

huangapple
  • 本文由 发表于 2023年7月13日 15:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定