Group and count in Rails
You can do count on line_items which will return you an ordered hash of device_id and count. @project.line_items.group(:device_id).count
You can do count on line_items which will return you an ordered hash of device_id and count. @project.line_items.group(:device_id).count
Yes, SELECT COUNT(*) FROM TableName
DatabaseUtils.queryNumEntries (since api:11) is useful alternative that negates the need for raw SQL(yay!). SQLiteDatabase db = getReadableDatabase(); DatabaseUtils.queryNumEntries(db, “users”, “uname=? AND pwd=?”, new String[] {loginname,loginpass});
how about; $ echo “100 100 100 99 99 26 25 24 24″ \ | tr ” ” “\n” \ | sort \ | uniq -c \ | sort -k2nr \ | awk ‘{printf(“%s\t%s\n”,$2,$1)}END{print}’ The result is : 100 3 99 2 26 1 25 1 24 2
Link to Working Examples Solution 0 This can be accompished using pivot tables. Solution 1 Use the unique formula to get all the distinct values. Then use countif to get the count of each value. See the working example link at the top to see exactly how this is implemented. Unique Values Count =UNIQUE(A3:A8) =COUNTIF(A3:A8;B3) … Read more
To count all occurrences, use -o. Try this: echo afoobarfoobar | grep -o foo | wc -l And man grep of course (: Update Some suggest to use just grep -co foo instead of grep -o foo | wc -l. Don’t. This shortcut won’t work in all cases. Man page says: -c print a count … Read more
Decompiling the source for the Count() extension method reveals that it tests whether the object is an ICollection (generic or otherwise) and if so simply returns the underlying Count property: So, if your code accesses Count instead of calling Count(), you can bypass the type checking – a theoretical performance benefit but I doubt it … Read more
The easiest way to do this, with excellent performance and compatibility with both old and new browsers, is to include either Lo-Dash or Underscore in your page. Then you can use either _.size(object) or _.keys(object).length For your obj.Data, you could test this with: console.log( _.size(obj.Data) ); or: console.log( _.keys(obj.Data).length ); Lo-Dash and Underscore are both … Read more
You need to alias the aggregate using the as keyword in order to call it from mysql_fetch_assoc $result=mysql_query(“SELECT count(*) as total from Students”); $data=mysql_fetch_assoc($result); echo $data[‘total’];
To get the number of votes for a specific item, you would use: vote_count = Item.objects.filter(votes__contest=contestA).count() If you wanted a break down of the distribution of votes in a particular contest, I would do something like the following: contest = Contest.objects.get(pk=contest_id) votes = contest.votes_set.select_related() vote_counts = {} for vote in votes: if not vote_counts.has_key(vote.item.id): vote_counts[vote.item.id] … Read more