1

I've got table with different actions from users, it looks like that

| User_ID       | Action        | 
| ------------- |:-------------:|
| 10            | View          | 
| 8             | Connect       | 
| 10            | View          |   
| 12            | View          |   
| 10            | View          |   

And i want to count how many users made "View" action. So i made Query

Actions.where( action: "View" ).group(:User_ID).count

But it showed me

{10=>3, 12=>1}

So I added second ".count" at the end

Actions.where( action: "View" ).group(:User_ID).count.count

And now it's working properly

Is double count here only and proper way?

Sky
  • 21
  • 2
  • What output do you want? Double counting is counting how many counts you have, which discards the counting in formation, so that seems like a waste of effort. – tadman Mar 03 '20 at 18:36
  • @tadman I want only number that shows double counting way – Sky Mar 03 '20 at 18:37
  • Can you explain what that means by way of a concrete example here? Do you want, literally, just `2`? – tadman Mar 03 '20 at 18:39
  • @tadman Yes, its for admin panel to count specific action per session but not count duplicates – Sky Mar 03 '20 at 18:39
  • 5
    Consider doing a [distinct count](https://stackoverflow.com/questions/36608/how-can-i-count-the-number-of-records-that-have-a-unique-value-in-a-particular-f) instead of this double-count. – tadman Mar 03 '20 at 18:40
  • @tadman sure! thank you! – Sky Mar 03 '20 at 18:42
  • Seems like this is solved, so either you (OP) or @tadman should write an answer – max pleaner Mar 03 '20 at 20:05

0 Answers0