Questions tagged [count]

Count refers to the number of objects in a collection. It's also a commonly-used SQL function that counts the number of rows.

Many programming languages offer some sort of count, length or size fields and/or methods for arrays and collections, e.g. PHP's count($array) or Java's array.length.

COUNT() is also commonly-used SQL function that counts the number of rows in a table. It is an ANSI SQL aggregate function that returns the number of times the argument is encountered per group (or if no non-aggregate columns, per query). Commonly, the argument is specified as * - ie COUNT(*) - simply counting the rows. It can be used to count distinct values of a column like this: COUNT(DISTINCT MY_COLUMN)

Reference

See also:

21460 questions
66
votes
7 answers

A method to count occurrences in a list

Is there a simple way to count the number of occurrences of all elements of a list into that same list in C#? Something like this: using System; using System.IO; using System.Text.RegularExpressions; using System.Collections.Generic; using…
Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84
65
votes
2 answers

How can I count how many duplicates there are for each distinct value in sqlite?

I have a table: ref,type 1,red 2,red 3,green 4,blue 5,black 6,black I want the result of a sqlite query to be: red,2 green,1 blue,1 black,2 I think the hardest thing to do is find a question to match my problem? Then I am sure the answer is…
Chris Denman
  • 1,187
  • 3
  • 9
  • 15
64
votes
4 answers

MySQL joins and COUNT(*) from another table

I have two tables: groups and group_members. The groups table contains all the information for each group, such as its ID, title, description, etc. In the group_members table, it lists all the members who are apart of each group like…
hohner
  • 11,498
  • 8
  • 49
  • 84
64
votes
5 answers

Python Count Elements in a List of Objects with Matching Attributes

I am trying to find a simple and fast way of counting the number of Objects in a list that match a criteria. e.g. class Person: def __init__(self, Name, Age, Gender): self.Name = Name self.Age = Age self.Gender =…
FacesOfMu
  • 927
  • 2
  • 7
  • 15
63
votes
5 answers

How to count non-empty entries in a PHP array?

Consider: [name] => Array ( [1] => name#1 [2] => name#2 [3] => name#3 [4] => name#4 [5] => [6] => [7] => [8] => …
Damon
  • 10,493
  • 16
  • 86
  • 144
63
votes
5 answers

Find the n most common values in a vector

I have a vector say c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7) How do I count each element, and then return the e.g. 3 most common elements, i.e. 1, 7, 5?
ChairmanMeow
  • 843
  • 3
  • 10
  • 12
62
votes
5 answers

How to count check-boxes using jQuery?

I have tons of checkboxes that are either checked (checked="checked") or unchecked. I would like to get the number of all checkboxes, unchecked and checked checkboxes. With check-box I mean . How can this be done with…
daGrevis
  • 21,014
  • 37
  • 100
  • 139
61
votes
7 answers

count lines in a PHP project

Do you know any tool which can count all the code lines from a PHP project?
Duncan Benoit
  • 3,297
  • 8
  • 29
  • 26
61
votes
8 answers

jQuery count number of divs with a certain class?

Considering something like this;
How would I, using jQuery (or plain JS,…
user825286
61
votes
2 answers

Conditional SQL count

What is the best way to create columns which count the number of occurrences of data in a table? The table needs to be grouped by one column? My database is PostgreSQL. I have seen: SELECT sum(CASE WHEN question1 = 0 THEN 1 ELSE 0 END) AS ZERO, …
user3542327
60
votes
4 answers

how to count new lines in a very big string?

The problem reduces to counting \n characters, so is there a function that can do it on a huge strings, since explode() wastes too much memory.
rsk82
  • 28,217
  • 50
  • 150
  • 240
60
votes
5 answers

Count number of files with certain extension in Python

I am fairly new to Python and I am trying to figure out the most efficient way to count the number of .TIF files in a particular sub-directory. Doing some searching, I found one example (I have not tested), which claimed to count all of the files in…
Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45
59
votes
5 answers

GROUP BY and COUNT in PostgreSQL

The query: SELECT COUNT(*) as count_all, posts.id as post_id FROM posts INNER JOIN votes ON votes.post_id = posts.id GROUP BY posts.id; Returns n records in Postgresql: count_all | post_id -----------+--------- 1 | 6 3 …
skinkelynet
  • 857
  • 2
  • 9
  • 14
58
votes
8 answers

How to count occurrences of a word in all the files of a directory?

I’m trying to count a particular word occurrence in a whole directory. Is this possible? Say for example there is a directory with 100 files all of whose files may have the word “aaa” in them. How would I count the number of “aaa” in all the files…
Ashish Sharma
  • 1,597
  • 7
  • 24
  • 35
58
votes
8 answers

In MongoDB's pymongo, how do I do a count()?

for post in db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num): How do I get the count()?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080