对在特定时间内创建的项目进行分组
我在过去几年创建的数据库中有一堆产品(500k 左右),我想将它们分组在一起(Rails 2.3.14)
将被视为同一组:
- 理想情况下,如果满足以下条件,它们 由同一家公司创建_id
- 它们是在 10 分钟内创建的
粗略地说明我想要完成的任务:
def self.package_products
Company.each do |company|
package = Package.new
products = Product.find(:all, :conditions => [:company_id = company && created_around_similar_times])
package.contents = first_few_product_descriptions
package.save!
products.update_all(:package_id => package.id)
end
end
但对我来说,它闻起来很糟糕。我不喜欢循环浏览这些公司,并且忍不住认为有更好的方法来做到这一点。有谁有任何可以对相似项目进行分组的sql-fu?基本上是寻找来自同一家公司且在 10 分钟内创建的产品,并为它们分配相同的 package_id。
I have a bunch of products (500k or so) in a database that were created over the last several years and I'd like to group them together (Rails 2.3.14)
Ideally, they would be considered the same group if:
- They were created by the same company_id
- They were created within 10 minutes of each other
A rough pass at what I'm trying to accomplish:
def self.package_products
Company.each do |company|
package = Package.new
products = Product.find(:all, :conditions => [:company_id = company && created_around_similar_times])
package.contents = first_few_product_descriptions
package.save!
products.update_all(:package_id => package.id)
end
end
To me it smells bad though. I don't like looping through the companies and can't help but think there's a better way to do it. Does anyone have any sql-fu that can group similar items? Basically looking to find products from the same company that were created within 10 minutes of each other and assign them the same package_id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在纯 SQL 中很难做到。我会诉诸 plpgsql 过程。
比如说,你的桌子看起来像这样:
(下次,请发布一个表定义。值一千多字。)
使用这样的 plpgsql 函数:
调用一次,然后丢弃:
现在,组中的所有成员都为根据您的定义共享一个
group_id
。在问题编辑后编辑
我添加了更多内容:
for_sale
,因为它不再出现在问题中。This is hard to to in pure SQL. I would resort to a plpgsql procedure.
Say, your table looks like this:
(Next time, be so nice as to post a table definition. Worth more than a thousand words.)
Use a plpgsql function like this:
Call once, then discard:
Now, all members of a group as per your definition share a
group_id
.Edit after question edit
I put in a couple more things:
for_sale
ignored in query after it's not in the question any more.