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
58
votes
7 answers

COUNT(*) from multiple tables in MySQL

How do I go about selecting COUNT(*)s from multiple tables in MySQL? Such as: SELECT COUNT(*) AS table1Count FROM table1 WHERE someCondition JOIN?? SELECT COUNT(*) AS table2Count FROM table2 WHERE someCondition CROSS JOIN? subqueries? SELECT…
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
58
votes
4 answers

Order by COUNT per value

I have a table which stores IDs and the city where the store is located. I want to list all the stores starting with the stores that are in the city where there are the most stores. TABLE ID CITY 1 NYC 2 BOS 3 BOS 4 NYC 5 NYC The output I want…
Enkay
  • 1,898
  • 6
  • 24
  • 35
56
votes
7 answers

How to count differences between two files on linux?

I need to work with large files and must find differences between two. And I don't need the different bits, but the number of differences. To find the number of different rows I come up with diff --suppress-common-lines --speed-large-files -y File1…
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
54
votes
1 answer

Trying to get the average of a count resultset

I have the following SQL:(bitemp) SELECT COUNT (*) AS Count FROM Table T WHERE (T.Update_time = (SELECT MAX (B.Update_time ) FROM Table B WHERE (B.Id = T.Id)) GROUP BY T.Grouping now I am getting a resultset…
Xavjer
  • 8,838
  • 2
  • 22
  • 42
54
votes
11 answers

How to count each digit in a range of integers?

Imagine you sell those metallic digits used to number houses, locker doors, hotel rooms, etc. You need to find how many of each digit to ship when your customer needs to number doors/houses: 1 to 100 51 to 300 1 to 2,000 with zeros to the left The…
Carlos Gutiérrez
  • 13,972
  • 5
  • 37
  • 47
53
votes
2 answers

Getting the size of an array in an object

I would like some help with getting the size of an array inside an object: var st = { "itema":{...},"itemb":[{"id":"s01","cd":"c01","dd":"d01",....}{"id":"s02","cd":"c02","dd":"d02",....}]} How would you get a count of the objects inside "itemb"…
meli
  • 533
  • 1
  • 4
  • 5
53
votes
9 answers

How to count how many values per level in a given factor?

I have a data.frame mydf with about 2500 rows. These rows correspond to 69 classes of objects in colum 1 mydf$V1, and I want to count how many rows per object class I have. I can get a factor of these classes with: objectclasses =…
Escher
  • 5,418
  • 12
  • 54
  • 101
52
votes
2 answers

Django Count() in multiple annotations

Say I have a simple forum model: class User(models.Model): username = models.CharField(max_length=25) ... class Topic(models.Model): user = models.ForeignKey(User) ... class Post(models.Model): user = models.ForeignKey(User) …
user749618
  • 1,170
  • 2
  • 11
  • 19
51
votes
9 answers

How to count the number of occurrences of a character in an Oracle varchar value?

How can I count number of occurrences of the character - in a varchar2 string? Example: select XXX('123-345-566', '-') from dual; ---------------------------------------- 2
Ula Krukar
  • 12,549
  • 20
  • 51
  • 65
51
votes
8 answers

How to get the row count of a query in Android using SQLite?

How do I get the row count of a query in Android using SQLite? It seems my following method does not work. public int getFragmentCountByMixId(int mixId) { int count = 0; SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); Cursor…
David
  • 2,691
  • 7
  • 38
  • 50
51
votes
3 answers

Number of items in Redis set

What's the easiest way to getting the number (count) of items in Redis set? Preferably without the need to dump whole set and count the lines... So far, I have found only BITCOUNT, which I have not found that useful...
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
51
votes
2 answers

How can I count runs in a sequence?

In R, what would be the most efficient/simplest way to count runs of identical elements in a sequence? For example, how to count the numbers of consecutive zeros in a sequence of non-negative integers: x <- c(1,0,0,0,1,0,0,0,0,0,2,0,0) # should give…
andrekos
  • 2,822
  • 4
  • 27
  • 26
51
votes
5 answers

Speeding up row counting in MySQL

Suppose, for illustrative purposes, you are running a library using a simple MySQL "books" table with three columns: (id, title, status) id is the primary key title is the title of the book status could be an enum describing the book's current…
Kevin Ivarsen
  • 1,019
  • 1
  • 8
  • 13
50
votes
8 answers

Getting a count of rows in a datatable that meet certain criteria

I have a datatable, dtFoo, and would like to get a count of the rows that meet a certain criteria. EDIT: This data is not stored in a database, so using SQL is not an option. In the past, I've used the following two methods to accomplish…
Sesame
  • 3,370
  • 18
  • 50
  • 75
50
votes
6 answers

Return total number of files in directory and subdirectories

Trying to create a function that returns the # of files found a directory and its subdirectories. Just need help getting started
Bob
  • 539
  • 2
  • 5
  • 11