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
97
votes
8 answers

count distinct values in spreadsheet

I have a Google spreadsheet with a column that looks like this: City ---- London Paris London Berlin Rome Paris I want to count the appearances of each distinct city (so I need the city name and the number of appearances). City |…
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
97
votes
7 answers

counting duplicates in a sorted sequence using command line tools

I have a command (cmd1) that greps through a log file to filter out a set of numbers. The numbers are in random order, so I use sort -gr to get a reverse sorted list of numbers. There may be duplicates within this sorted list. I need to find the…
letronje
  • 9,002
  • 9
  • 45
  • 53
93
votes
10 answers

SQLite Query in Android to count rows

I'm trying to create a simple Login form, where I compare the login id and password entered at the login screen with that stored in the database. I'm using the following query: final String DATABASE_COMPARE = "select count(*) from users where…
arun
  • 1,001
  • 2
  • 8
  • 5
93
votes
2 answers

Counting the number of missing/NaN in each row

I've got a dataset with a big number of rows. Some of the values are NaN, like this: In [91]: df Out[91]: 1 3 1 1 1 1 3 1 1 1 2 3 1 1 1 1 1 NaN NaN NaN 1 3 1 1 …
Chernyavski.aa
  • 1,153
  • 1
  • 9
  • 16
86
votes
3 answers

Add count of unique / distinct values by group to the original data

I wish to count the number of unique values by grouping of a second variable, and then add the count to the existing data.frame as a new column. For example, if the existing data frame looks like this: color type 1 black chair 2 black chair 3…
Bryan
  • 1,771
  • 4
  • 17
  • 30
85
votes
7 answers

Group and count in Rails

I know I've seen this before but I can't find anything now. I want to group a query by a certain column and be able to display how many are in each group. I got the first part down: @line_items = @project.line_items.all(:group => "device_id") …
tladuke
  • 1,337
  • 2
  • 11
  • 22
84
votes
8 answers

How do I add two count(*) results together on two different tables?

I have two tables: Toys and Games. +--------------------+------------------+ | Field | Type | +--------------------+------------------+ | toy_id | int(10) unsigned | | little_kid_id | int(10) unsigned…
Runcible
  • 7,006
  • 12
  • 42
  • 62
84
votes
8 answers

Conditional Count on a field

If I had a table like this: jobId, jobName, Priority Whereby Priority can be an integer between 1 to 5. Since I would need this query for generating a chart on report, I would need to display the jobid, jobname and 5 fields called Priority1,…
Houman
  • 64,245
  • 87
  • 278
  • 460
82
votes
3 answers

How to count items in JSON data

How I can get the number of elements in node of JSON data? JSON: { "result":[ { "run":[ { "action":"stop" }, { "action":"start" }, { "action":"start" } …
mierzej
  • 843
  • 1
  • 6
  • 4
82
votes
3 answers

Ruby: How to count the number of times a string appears in another string?

I'm trying to count the number of times a string appears in another string. I know you can count the number of times a letter appears in a string: string = "aabbccddbb" string.count('a') => 2 But if I search for how many times 'aa' appears in this…
Johnson
  • 1,679
  • 1
  • 14
  • 21
81
votes
7 answers

Count occurrences of False or True in a column in pandas

given patient_id test_result has_cancer 0 79452 Negative False 1 81667 Positive True 2 76297 Negative False 3 36593 Negative False 4 53717 Negative False 5 67134 Negative False 6 40436 Negative …
Ney J Torres
  • 1,611
  • 3
  • 12
  • 14
81
votes
3 answers

Using GroupBy, Count and Sum in LINQ Lambda Expressions

I have a collection of boxes with the properties weight, volume and owner. I want to use LINQ to get a summarized list (by owner) of the box information e.g. **Owner, Boxes, Total Weight, Total Volume** Jim, 5, 1430.00, 3.65 George,…
Jimbo
  • 22,379
  • 42
  • 117
  • 159
79
votes
3 answers

Count the Number of Tables in a SQL Server Database

I have a SQL Server 2012 database called MyDatabase. How can I find how many tables are in the database? I'm assuming the format of the query would be something like the following, but I don't know what to replace database_tables with: USE…
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
78
votes
4 answers

Counting number of joined rows in left join

I'm trying to write an aggregate query in SQL which returns the count of all records joined to a given record in a table; If no records were joined to the given record, then the result for that record should be 0: Data My database looks like this…
errantlinguist
  • 3,658
  • 4
  • 18
  • 41
74
votes
7 answers

How can I count the number of elements of a given value in a matrix?

Does anyone know how to count the number of times a value appears in a matrix? For example, if I have a 1500 x 1 matrix M (vector) which stores the values of weekdays (1 - 7), how could I count how many Sundays (1), Mondays(2), ... , Saturdays(7)…
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286