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
74
votes
9 answers

MySQL - How to count all rows per table in one query

Is there a way to query the DB to find out how many rows there are in all the tables? i.e. table1 1234 table2 222 table3 7888 Hope you can advise
Lee
  • 20,034
  • 23
  • 75
  • 102
73
votes
10 answers

Faster alternative in Oracle to SELECT COUNT(*) FROM sometable

I've notice that in Oracle, the query SELECT COUNT(*) FROM sometable; is very slow for large tables. It seems like the database it actually going through every row and incrementing a counter one at a time. I would think that there would be a…
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
71
votes
6 answers

Counting number of grouped rows in mysql

In a table xyz I have a row called components and a labref row which has labref number as shown here Table xyz labref component NDQA201303001 a NDQA201303001 a NDQA201303001 a NDQA201303001 …
alphy
  • 931
  • 4
  • 13
  • 23
70
votes
2 answers

SQL COUNT overflow

Here is my query: SELECT COUNT(*) FROM Similarities WHERE T1Similarity = 0 OR T2Similarity = 0 Here is the result: Msg 8115, Level 16, State 2, Line 1 Arithmetic overflow error converting expression to data type int. The table has 4 billion rows.…
user593062
  • 1,593
  • 4
  • 15
  • 24
70
votes
7 answers

How do you access the value of an SQL count () query in a Java program

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in this case when there is no specific column name. I have used 'AS' in the…
Ankur
  • 50,282
  • 110
  • 242
  • 312
69
votes
14 answers

Item frequency count in Python

Assume I have a list of words, and I want to find the number of times each word appears in that list. An obvious way to do this is: words = "apple banana apple strawberry banana lemon" uniques = set(words.split()) freqs = [(item,…
Daniyar
  • 1,680
  • 2
  • 15
  • 23
67
votes
5 answers

MongoDB 'count()' is very slow. How do we refine/work around with it?

I am currently using MongoDB with millions of data records. I discovered one thing that's pretty annoying. When I use 'count()' function with a small number of queried data collection, it's very fast. However, when the queried data collection…
Winston Chen
  • 6,799
  • 12
  • 52
  • 81
67
votes
11 answers

Count number of rows per group and add result to original data frame

Say I have a data.frame object: df <- data.frame(name=c('black','black','black','red','red'), type=c('chair','chair','sofa','sofa','plate'), num=c(4,5,12,4,3)) Now I want to count the number of rows (observations)…
Uri Laserson
  • 2,391
  • 5
  • 30
  • 39
67
votes
1 answer

Include levels of zero count in result of table()

I have a vector 'y' and I count the different values using table: y <- c(0, 0, 1, 3, 4, 4) table(y) # y # 0 1 3 4 # 2 1 1 2 However, I also want the result to include the fact that there are zero 2's and zero 5's. Can I use table() for…
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
67
votes
13 answers

How do I count occurrence of duplicate items in array

I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences. Here is my code; BUT I don't where am going wrong!
mukamaivan
  • 1,435
  • 6
  • 16
  • 24
67
votes
10 answers

PHP - count specific array values

How can I count the number of element inside an array with value equals a constant? example, $myArray = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben"); how can I directly know how many "Ben" is inside?
Ivo San
  • 1,233
  • 4
  • 16
  • 26
67
votes
4 answers

SQL COUNT* GROUP BY bigger than,

I want to select the distinct keys with the occurence number, this query seems functionate: SELECT ItemMetaData.KEY, ItemMetaData.VALUE, count(*) FROM ItemMetaData GROUP BY ItemMetaData.KEY ORDER BY count(*) desc; But I also want to filter these…
czupe
  • 4,740
  • 7
  • 34
  • 52
66
votes
12 answers

Most efficient way to get table row count

I currently have a database with over 6 million rows and growing. I currently do SELECT COUNT(id) FROM table; in order to display the number to my users, but the database is getting large and I have no need to store all of those rows except to be…
James Simpson
  • 13,488
  • 26
  • 83
  • 108
66
votes
7 answers

Count the number of times a string appears within a string

I simply have a string that looks something like this: "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false" All I want to do is to count how many times the string "true" appears in that string. I'm feeling like the answer is…
Paul Mignard
  • 5,824
  • 6
  • 44
  • 60
66
votes
2 answers

Python pandas: Add a column to my dataframe that counts a variable

I have a dataframe 'gt' like this: org group org1 1 org2 1 org3 2 org4 3 org5 3 org6 3 and I would like to add column 'count' to gt dataframe to counts number member of the groups, expected results like this: org …
UserYmY
  • 8,034
  • 17
  • 57
  • 71