英文:
Active Record Query to filter without duplicate records
问题
以下是翻译好的部分:
"Could anyone help me to filter the records without duplicates?"
"Here is the PurchaseOrder table with id, job_number, descriptiom and order number fields. If I search a job number 348282227 from this table we can see here line number 2 with id 6678 and 6686 is duplicated with same description and order number. How can I get results without 6686 row."
"以下是PurchaseOrder表格,包括id、job_number、description和order number字段。如果我从这个表格中搜索工作编号348282227,我们可以看到在这里,行号2的id为6678和6686,它们的description和order number是相同的。我如何获取不包括6686行的结果。"
"+-------+------------+------------------------------+--------------+
| id | job_number | description | order_number |
+-------+------------+------------------------------+--------------+
| 6677 | 348282227 | Moisture Inspection 1 | 224201 |
| 6678 | 348282227 | Inspection 3 | 224202 |
| 6679 | 348282227 | Moisture Inspection Final | 224203 |
| 6680 | 348282227 | BFP Certification Inspection | 224204 |
| 6681 | 348282227 | Structural Engineering | 224206 |
| 6682 | 348282227 | Frame Labor 8 | 224218 |
| 6683 | 348282227 | Stucco Turnkey #3 | 224244 |
| 6684 | 348282227 | Retention - Stucco | 224245 |
| 6685 | 348282227 | Roofing Turnkey | 224246 |
| 6686 | 348282227 | Inspection 3 | 224202 |
+-------+------------+------------------------------+--------------+"
"已经尝试了下面的查询,但不起作用。"
"试图使用以下查询,但没有成功。"
uniq_order_numbers = PurchaseOrder.where(job_number: "348282227").pluck(:order_number).uniq
PurchaseOrder.where(order_number: uniq_order_numbers).select(:job_number, :description, :order_number)
"Can anyone help me for this?"
"有人可以帮助我吗?"
英文:
Could anyone help me to filter the records without duplicates
Here is the PurchaseOrder table with id, job_number, descriptiom and order number fields. If I search a job number 348282227 from this table we can see here line number 2 with id 6678 and 6686 is duplicated with same description and order number. How can I get results without 6686 row.
+-------+------------+------------------------------+--------------+
| id | job_number | description | order_number |
+-------+------------+------------------------------+--------------+
| 6677 | 348282227 | Moisture Inspection 1 | 224201 |
| 6678 | 348282227 | Inspection 3 | 224202 |
| 6679 | 348282227 | Moisture Inspection Final | 224203 |
| 6680 | 348282227 | BFP Certification Inspection | 224204 |
| 6681 | 348282227 | Structural Engineering | 224206 |
| 6682 | 348282227 | Frame Labor 8 | 224218 |
| 6683 | 348282227 | Stucco Turnkey #3 | 224244 |
| 6684 | 348282227 | Retention - Stucco | 224245 |
| 6685 | 348282227 | Roofing Turnkey | 224246 |
| 6686 | 348282227 | Inspection 3 | 224202 |
+-------+------------+------------------------------+--------------+
Tried the below query but not works.
uniq_order_numbers = PurchaseOrder.where(job_number: "348282227").pluck(:order_number).uniq
PurchaseOrder.where(order_number: uniq_order_numbers).select(:job_number, :description, :order_number)
Can anyone help me for this.
答案1
得分: 1
你可以按照dbugger的建议选择唯一路线:
https://www.rubyguides.com/2019/08/ruby-uniq-method/
但真正的问题是你的数据库中存在重复数据。你可能不应该允许这种情况发生 - 要么在记录级别,要么(最好的情况下)在数据库级别进行限制。
https://guides.rubyonrails.org/active_record_validations.html#uniqueness
英文:
You can go the uniq route, as dbugger suggests:
https://www.rubyguides.com/2019/08/ruby-uniq-method/
But the real issue is that you have duplicate data in your database. You should probably not be permitting that - either at the record level or/also (ideally) at the database level.
https://guides.rubyonrails.org/active_record_validations.html#uniqueness
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论