postgresql 对具有相同hashTag名称的散列标签进行分组

e4eetjau  于 2023-01-13  发布在  PostgreSQL
关注(0)|答案(1)|浏览(93)

我在组标签中发现了一个问题:有什么解决办法吗?
下面是我查询语句:任何帮助. def趋势标记(用户)

@tags= HashTag.joins(:vibe_hash_tags).limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 1.months.ago, Time.now).select("hash_tags.id, hash_tags.address as country, count(vibe_hash_tags.id) as vibes_hash_tags,COUNT(hash_tags.id)as hash_tag_count")
     .group('COUNT(hash_tags.name as hash_tag_name)')
 render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => @tags}}
end

我想把它归为
{“成功”:真,“结果”:{“趋势散列标签”:[

{
            "id": 83,
            "country": "TV",
            "vibes_hash_tags": 2,
            "hash_tag_name": "money",
            "hash_tag_count": 2
        },
        {
            "id": 84,
            "country": "TV",
            "vibes_hash_tags": 3,
            "hash_tag_name": "grace",
            "hash_tag_count": 3
        }
    ]
}

}

ddrv8njm

ddrv8njm1#

我已经能够找到一个解决我的问题分组哈希标签.

def trending_tags
   @hash_tags= HashTag
  .select('hash_tags.name AS hash_tag_name, COUNT(hash_tags
  .id) AS hash_tag_count, hash_tags.address AS country, count(vibe_hash_tags.id) AS vibes_hash_tags')
  .joins(:vibe_hash_tags)
  .order('hash_tag_count DESC')
  .group('hash_tags
  .name, hash_tags.address').limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 5.days.ago, Time.now)
 render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => @hash_tags}}
    end

相关问题