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
50
votes
6 answers

Multiple aggregate functions in HAVING clause

Due to the nature of my query i have records with counts of 3 that would also fit the criteria of having count of 2 and so on. I was wondering is it possible to query 'having count more than x and less than 7' ? How could I write this. Here is my…
blarg
  • 3,773
  • 11
  • 42
  • 71
50
votes
5 answers

SQL Server Count is slow

Counting tables with large amount of data may be very slow, sometimes it takes minutes; it also may generate deadlock on a busy server. I want to display real values, NOLOCK is not an option. The servers I use is SQL Server 2005 or 2008 Standard or…
Adi
  • 5,113
  • 6
  • 46
  • 59
50
votes
6 answers

SQL count(*) performance

I have a SQL table BookChapters with over 20 millions rows. It has a clustered primary key (bookChapterID) and doesn't have any other keys or indexes. It takes miliseconds to run the following query if (select count(*) from BookChapters) =…
danmiao
  • 747
  • 2
  • 8
  • 17
49
votes
5 answers

What's the equivalent of Panda's value_counts() in PySpark?

I am having the following python/pandas command: df.groupby('Column_Name').agg(lambda x: x.value_counts().max() where I am getting the value counts for ALL columns in a DataFrameGroupBy object. How do I do this action in PySpark?
TSAR
  • 683
  • 1
  • 6
  • 8
49
votes
6 answers

How To Count Associated Entities Without Fetching Them In Entity Framework

I've been wondering about this one for a while now, so I thought it would be worth using my first Stack Overflow post to ask about it. Imagine I have a discussion with an associated list of messages: DiscussionCategory discussionCategory =…
Oligarchia
  • 493
  • 1
  • 4
  • 6
48
votes
4 answers

C# EPPlus OpenXML count rows

With EPPlus and OpenXML does anyone know the syntax on how to count the rows? Say my worksheet is called "worksheet" int numberRows = worksheet.rows.count()? or worksheet.rows.dimension I'm certainly interested in the answer, but how to find the…
rd42
  • 3,584
  • 15
  • 56
  • 68
48
votes
6 answers

Row count of a column family in Cassandra

Is there a way to get a row count (key count) of a single column family in Cassandra? get_count can only be used to get the column count. For instance, if I have a column family containing users and wanted to get the number of users. How could I do…
Henri Liljeroos
  • 511
  • 1
  • 4
  • 4
48
votes
1 answer

SQL: Multiple count statements with different criteria

I was just wondering if there is any way to get two separate "count" totals from a table using one query? That is, using a table similar to the following I would like to retrieve each code (distinct) and show the total number of status' NOT equal to…
jj2
  • 916
  • 1
  • 8
  • 19
47
votes
6 answers

Count indexes using "for" in Python

I need to do in Python the same as: for (i = 0; i < 5; i++) {cout << i;} but I don't know how to use FOR in Python to get the index of the elements in a list.
Lucas Rezende
  • 2,516
  • 8
  • 25
  • 34
47
votes
5 answers

Count and Sort with Pandas

I have a dataframe for values form a file by which I have grouped by two columns, which return a count of the aggregation. Now I want to sort by the max count value, however I get the following error: KeyError: 'count' Looks the group by agg count…
Rubans
  • 4,188
  • 6
  • 40
  • 58
47
votes
6 answers

T-SQL Count rows with specific values (Multiple in one query)

I need some help with a T-SQL query. I want to count fields that have a special value(e.g. >1). Assuming i have a table like: IGrp | Item | Value1 | Value2 ############################# A | I11 | 0.52 | 1.18 A | I12 | 1.30 | 0.54 A |…
Marks
  • 3,613
  • 5
  • 31
  • 46
46
votes
6 answers

Extend contigency table with proportions (percentages)

I have a contingency table of counts, and I want to extend it with corresponding proportions of each group. Some sample data (tips data set from ggplot2 package): library(ggplot2) head(tips, 3) # total_bill tip sex smoker day time size # 1 …
userJT
  • 11,486
  • 20
  • 77
  • 88
46
votes
2 answers

Query with LEFT JOIN not returning rows for count of 0

I am trying to get the following to return a count for every organization using a left join in PostgreSQL, but I cannot figure out why it's not working: select o.name as organisation_name, coalesce(COUNT(exam_items.id)) as total_used …
mulus
  • 475
  • 1
  • 4
  • 7
46
votes
7 answers

Mysql count performance on very big tables

I have a table with more than 100 millions rows in Innodb. I have to know if there is more than 5000 rows where the foreign key = 1. I don't need the exact number. I made some testing : SELECT COUNT(*) FROM table WHERE fk = 1 => 16 seconds SELECT…
hotips
  • 2,575
  • 7
  • 42
  • 59
45
votes
4 answers

mysql: get record count between two date-time

I am stuck with a problem in MySQL. I want to get the count of records between two date-time entries. For example: I have a column in my table named 'created' having the datetime data type. I want to count records which were created date-time…
gautamlakum
  • 11,815
  • 23
  • 67
  • 90