Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8348413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:40:54+00:00 2026-06-09T07:40:54+00:00

Say i queried table rawStats = Stats::TrackProcessed.select(token) and received number of all tokens in

  • 0

Say i queried table

rawStats = Stats::TrackProcessed.select("token")

and received number of all tokens in a table.

Looks like this:

3e79a387c29bda1069271e06ad03d82b8296242e
059681f46ab1c1fa8cf8443a82f0898172e0b646
eacd846ea4e91b49f92f416f61e0f2d075b9dae7
eacd846ea4e91b49f92f416f61e0f2d075b9dae7
811705019a970929801adbf3db0ede31ed01816c

I need to return a hash list that will look like this

{
   '3e79a387c29bda1069271e06ad03d82b8296242e' => 1,
   '059681f46ab1c1fa8cf8443a82f0898172e0b646' => 1
   'eacd846ea4e91b49f92f416f61e0f2d075b9dae7' => 2
   '811705019a970929801adbf3db0ede31ed01816c' => 1
}

Where first value is a token and a second, number of occurances of this token in a table.
I then need to get an average value of this from all records.

So far i have gotten with querying table with

rawStats = Stats::TrackProcessed.select("distinct token")

By retrieving unique tokens

And then though each loop count number of occurrences for each token

rawArr = []
rawStats.each do |r|
  token = {
    :token => r.token,
    :count => rawStats.where("token = ?",r.token).count('token')
  }

  rawArr << token
end

But this processes as

(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '196e595b573f71fc2af04693c73809303bebd62d'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'db67ab44e94ca338d90e902a36c37b4998a47ff0'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'a8a78cffc0935b07f90b5f3008dcad27e8ac71c7'
(0.8ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '91b8ae5ffa12ece30d8548b488fab8aff7614f2b'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '649e7aa59cc8af6c59cec1cf637ffed1ce6b3be7'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '441dae6f22776687b57daaaeef2c63c31902b987'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '6b8937a13012f22c8a4cdd4ed4caad0ad8761d3b'
(0.6ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'beaba154f38d5c9b64e4fb2d851ec785bd6bc4ec'
(0.6ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'efa9f3a71c4ff33abcae4af788ef6d0be599f76a'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '053a7727885f3c4099a9b31a2eccc77b99df50ee'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = '32dc984c9f85ff91242533144cd179a9b4529bed'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'a58804ae38490f08a1645c8271e88df3757fe771f'
(0.7ms)  SELECT COUNT(`stats_track_processed`.`token`) FROM `stats_track_processed` WHERE `stats_track_processed`.`token` = 'a58804a2e38490f08a1645c8271e88df3757fe771f'

Which really looks like a very bad idea by querying each token separately when processing a large amount of data.

I had this done via SQL query already, looks like this

SELECT AVG(viewsAverage) as total 
FROM 
 (SELECT COUNT(token) AS viewsAverage 
  FROM stats_track_processed 
  WHERE admedia_id = #{params[:admedia_id]} 
    AND #{params[:banner_id]} 
    AND (access_time BETWEEN '#{params[:begin]}' 
    AND DATE_ADD('#{params[:end]}', INTERVAL 1 DAY)) 
  GROUP BY token) 
stats_track_processed

But this really looks like a crappy solution and i am refactoring code at the moment and moving all the logics to the controller.

Any help will be much appreciated.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:40:56+00:00Added an answer on June 9, 2026 at 7:40 am

    Iterate through the full list and maintain a hash of the counts.

    result = Stats::TrackProcessed.select("token")
    counts = Hash.new{|h,k| h[k] = 0 }
    result.each{|el| counts[el] +=1}
    

    Then to get an average you could do something like

     average = counts.values.inject(0){|sum, el| sum + el} / counts.length
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say my table looks like this: Sessions start_dts (datetime) end_dts (datetime) and the
Let's say I have table like this: id: PRIMARY INT price: INT (in dollars)
Let's say you've got a table like this: ID Cat1 Cat2 1 a red
Let's say I have table with column 'URL' whrere I store urls like this
Say I have a table for students attending school like this: St_ID |St_Name|Class_ID|Year ------+-------+--------+----
In other words, let's say this is my table: tbl_rel id groupID created Index
Say I have a table called students, containing a list of all the students.
The first thing i would like to say is that this is not exactly
Ok lets say I'm making a social networks like twitter. I have a table
For simplicity's sake, let's say we have this rather contrived table: [ID] [Weekday] [Weather]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.