开发者

Ruby loop to create an array of DISTINCT counts

开发者 https://www.devze.com 2023-04-02 01:16 出处:网络
I\'m trying to create an array of counts per day. I want the counts to be only of distinct uid\'s (what ui开发者_C百科d\'s are \"distinct\" shouldn\'t be reset each day).

I'm trying to create an array of counts per day. I want the counts to be only of distinct uid's (what ui开发者_C百科d's are "distinct" shouldn't be reset each day).

Before, I had:

 @unique_count_array_by_day = []
    15.times { |i| 
      bar = Model.select("DISTINCT(uid)").where(:created_at => (Time.now.beginning_of_day - i.days)..(Time.now.beginning_of_day - (i-1).days)).count()
      @unique_count_array_by_day << bar
    }

This wasn't giving me distinct uid's overall, it was giving me the count of unique uid's within a day. So I pulled the code selecting the distinct uid's out of the loop:

 @unique_count_array_by_day = []
  foo = Model.select("DISTINCT(uid)")
    15.times { |i| 
      bar = foo.where(:created_at => (Time.now.beginning_of_day - i.days)..(Time.now.beginning_of_day - (i-1).days)).count()
      @unique_count_array_by_day << bar
    }

However, this still produces a count of distinct uid's per day instead of distinct uid's on their first occurrence in the data table.

Any thoughts on how to finagle this?


If you just want a list of distinct ID's you should just remove the loop:

@unique_uids = Model.select("DISTINCT(uid)").all

If you want to get the date that a uid first occurs, you could do something like this:

@unique_uids_with_first_dates = Model.find(:select => 'uid, min(created_at)', :group => 'uid')

(untested, so not sure if that works as-is, but that's basically the way to do it)

Not sure if that totally answers your question, I was a little confused by "overall distincts"

0

精彩评论

暂无评论...
验证码 换一张
取 消