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
133
votes
14 answers

Laravel Eloquent - distinct() and count() not working properly together

So I'm trying to get the number of distinct pids on a query, but the returned value is wrong. This is what I try to do: $ad->getcodes()->groupby('pid')->distinct()->count() what returns the value "2", while the value it should return, should be…
Inigo EC
  • 2,178
  • 3
  • 22
  • 31
127
votes
6 answers

Count() vs len() on a Django QuerySet

In Django, given that I have a QuerySet that I am going to iterate over and print the results of, what is the best option for counting the objects? len(qs) or qs.count()? (Also given that counting the objects in the same iteration is not an option.)
antonagestam
  • 4,532
  • 3
  • 32
  • 44
125
votes
15 answers

How to count identical string elements in a Ruby array

I have the following Array = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"] How do I produce a count for each identical element? Where: "Jason" = 2, "Judah" = 3, "Allison" = 1, "Teresa" = 1, "Michelle" = 1? or…
user398520
  • 1,553
  • 3
  • 12
  • 11
124
votes
4 answers

Count number of non-NaN entries in every column of Dataframe

I have a massive DataFrame, and I was wondering if there was a short (one or two liner) way to get a count of non-NaN entries in a DataFrame. I don't want to do this one column at a time as I have close to 1000 columns. df1 =…
cryp
  • 2,285
  • 3
  • 26
  • 33
124
votes
5 answers

SQL to Entity Framework Count Group-By

I need to translate this SQL statement to a Linq-Entity query... SELECT name, count(name) FROM people GROUP by name
fefwfefefwfwe
  • 1,505
  • 2
  • 11
  • 19
122
votes
8 answers

Search for string and get count in vi editor

I want to search for a string and find the number of occurrences in a file using the vi editor.
kadeshpa
  • 1,607
  • 5
  • 18
  • 23
116
votes
5 answers

R: Count number of objects in list

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) next
Karl
  • 5,573
  • 8
  • 50
  • 73
114
votes
4 answers

Getting a count of objects in a queryset in Django

How can I add a field for the count of objects in a database. I have the following models: class Item(models.Model): name = models.CharField() class Contest(models.Model); name = models.CharField() class Votes(models.Model): user =…
thesteve
  • 2,413
  • 6
  • 26
  • 28
114
votes
5 answers

Get number of items from list (or other iterable) with certain condition

Assuming that I have a list with a huge number of items, l = [ 1, 4, 6, 30, 2, ... ] I want to get the number of items from that list, where an item satisfies a certain condition. My first thought was: count = len([i for i in l if…
cinsk
  • 1,576
  • 2
  • 12
  • 14
112
votes
7 answers

(How) can I count the items in an enum?

This question came to my mind, when I had something like enum Folders {FA, FB, FC}; and wanted to create an array of containers for each folder: ContainerClass*m_containers[3]; .... m_containers[FA] = ...; // etc. (Using maps it's much more…
fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
109
votes
12 answers

select count(*) from table of mysql in php

I am able to get both the value and row of the mysql query result. But I am struggling to get the single output of a query. e.g.: $result = mysql_query("SELECT COUNT(*) FROM Students;"); I need the result to display. But I am not getting the…
Gana
  • 1,093
  • 2
  • 8
  • 4
109
votes
3 answers

Javascript counting number of objects in object

I have an object something like: Object {0=Object, 1=Object, 2=Object} // Output from console.log(obj.Data); But there is no way that I can count the number of objects in object, then finally get the attribute value from the sub objects. I have…
Bryan Learn
  • 1,563
  • 3
  • 15
  • 13
106
votes
10 answers

Count property vs Count() method?

Working with a collection I have the two ways of getting the count of objects; Count (the property) and Count() (the method). Does anyone know what the key differences are? I might be wrong, but I always use the Count property in any conditional…
user1017882
100
votes
7 answers

SQL count rows in a table

I need to send a SQL query to a database that tells me how many rows there are in a table. I could get all the rows in the table with a SELECT and then count them, but I don't like to do it this way. Is there some other way to ask the number of the…
Celeste Capece
  • 1,306
  • 2
  • 11
  • 20
98
votes
5 answers

Count number of occurrences of a pattern in a file (even on same line)

When searching for number of occurrences of a string in a file, I generally use: grep pattern file | wc -l However, this only finds one occurrence per line, because of the way grep works. How can I search for the number of times a string appears in…
jrdioko
  • 32,230
  • 28
  • 81
  • 120