【问题标题】:Ruby Gem Sequel - how to show only group by with count value > 1Ruby Gem Sequel - 如何仅显示计数值 > 1 的分组
【发布时间】:2021-10-15 10:36:09
【问题描述】:

DB[:items].group_and_count(:name).all

我得到一个包含出现次数的名称列表。

如果我只希望返回的名称(包括其数量)超过例如2? 在 SQL 中,我会这样做:

SELECT name, count(name) FROM items GROUP BY name HAVING count(name) > 2

【问题讨论】:

    标签: ruby sequel


    【解决方案1】:

    这个呢?

    DB[:items].group('name').having('COUNT(name) > 2')
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • Almstost,必须使用 DB[:items].group('name').having{COUNT(name) > 2'} 才能正常工作。
    • @Muhammedogz 是的,我编辑了我的答案! ??
    【解决方案2】:

    您只需添加having子句,在Sequel中可以通过多种方式完成。

    # This uses virtual row as it is called in Sequel, with the { and }
    DB[:items].group_and_count(:name).having{Sequel.function(:count, :name) > 2}.all
    
    # or like this if you prefer
    DB[:items].group_and_count(:name).having{COUNT(name) > 2}.all
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-25
      • 2012-09-10
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多