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

Count number of occurences for each unique value

Let's say I have: v = rep(c(1,2, 2, 2), 25) Now, I want to count the number of times each unique value appears. unique(v) returns what the unique values are, but not how many they are. > unique(v) [1] 1 2 I want something that gives me…
gakera
  • 3,589
  • 4
  • 30
  • 36
182
votes
20 answers

Get number of digits with JavaScript

As the title of my post suggests, I would like to know how many digits var number has. For example: If number = 15; my function should return 2. Currently, it looks like this: function getlength(number) { return number.toString().length(); } But…
bit4fox
  • 2,021
  • 2
  • 13
  • 8
172
votes
20 answers

count (non-blank) lines-of-code in bash

In Bash, how do I count the number of non-blank lines of code in a project?
Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
172
votes
3 answers

Group by & count function in sqlalchemy

I want a "group by and count" command in sqlalchemy. How can I do this?
Nazmul Hasan
  • 6,840
  • 13
  • 36
  • 37
171
votes
3 answers

Linq with group by having count

how do I write this query in linq (vb.net)? select B.Name from Company B group by B.Name having COUNT(1) > 1
Fernando
  • 2,123
  • 4
  • 17
  • 21
163
votes
21 answers

How to obtain the total numbers of rows from a CSV file in Python?

I'm using python (Django Framework) to read a CSV file. I pull just 2 lines out of this CSV as you can see. What I have been trying to do is store in a variable the total number of rows the CSV also. How can I get the total number of rows? file =…
GrantU
  • 6,325
  • 16
  • 59
  • 89
160
votes
5 answers

Count with IF condition in MySQL query

I have two tables, one is for news and the other one is for comments and I want to get the count of the comments whose status has been set as approved. SELECT ccc_news . *, count(if(ccc_news_comments.id = 'approved', ccc_news_comments.id,…
user1163513
  • 4,087
  • 7
  • 24
  • 25
155
votes
5 answers

count(*) vs count(column-name) - which is more correct?

Does it make a difference if you do count(*) vs count(column-name) as in these two examples? I have a tendency to always write count(*) because it seems to fit better in my mind with the notion of it being an aggregate function, if that makes sense.…
bread
  • 1,619
  • 2
  • 11
  • 6
153
votes
17 answers

How to count instances of character in SQL Column

I have an sql column that is a string of 100 'Y' or 'N' characters. For example: YYNYNYYNNNYYNY... What is the easiest way to get the count of all 'Y' symbols in each row.
cindi
  • 4,571
  • 8
  • 31
  • 38
152
votes
9 answers

PHP: Count a stdClass object

I have a stdClass object created from json_decode that won't return the right number when I run the count($obj) function. The object has 30 properties, but the return on the count() function is say 1. Any ideas? Below is an example of one of the…
hellopat
  • 1,799
  • 2
  • 14
  • 14
144
votes
4 answers

In Firebase, is there a way to get the number of children of a node without loading all the node data?

You can get the child count via firebase_node.once('value', function(snapshot) { alert('Count: ' + snapshot.numChildren()); }); But I believe this fetches the entire sub-tree of that node from the server. For huge lists, that seems RAM and latency…
josh
  • 9,038
  • 8
  • 31
  • 37
140
votes
4 answers

Lists: Count vs Count()

Given a list, which method is preferred to determine the number of elements inside? var myList = new List(); myList.Count myList.Count()
Saxman
  • 5,009
  • 11
  • 51
  • 72
140
votes
5 answers

Count occurrences of a char in plain text file

Is there any way under linux/terminal to count, how many times the char f occurs in a plain text file?
cupakob
  • 8,411
  • 24
  • 67
  • 76
134
votes
5 answers

Count number of rows by group using dplyr

I am using the mtcars dataset. I want to find the number of records for a particular combination of data. Something very similar to the count(*) group by clause in SQL. ddply() from plyr is working for me library(plyr) ddply(mtcars,…
charmee
  • 1,501
  • 2
  • 9
  • 9
134
votes
15 answers

Count how many files in directory PHP

I'm working on a slightly new project. I wanted to know how many files are in a certain directory.