英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论