COUNT([DISTINCT] { | *}) : COUNT « Aggregate Functions « SQL / MySQL






COUNT([DISTINCT] { | *})

       
mysql>
mysql> CREATE TABLE Classes
    -> (
    ->     ClassID SMALLINT NOT NULL PRIMARY KEY,
    ->     Dept CHAR(4) NOT NULL,
    ->     Level ENUM('Upper', 'Lower') NOT NULL,
    ->     TotalStudents TINYINT UNSIGNED NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO Classes VALUES
    -> (1001, 'ANTH', 'Upper', 25),
    -> (1002, 'ANTH', 'Upper', 25),
    -> (1003, 'MATH', 'Upper', 18),
    -> (1004, 'ANTH', 'Lower', 19),
    -> (1005, 'ENGL', 'Upper', 28),
    -> (1006, 'MATH', 'Lower', 23),
    -> (1007, 'ENGL', 'Upper', 25),
    -> (1008, 'MATH', 'Lower', 29),
    -> (1009, 'ANTH', 'Upper', 25),
    -> (1010, 'ANTH', 'Lower', 30),
    -> (1011, 'ENGL', 'Lower', 26),
    -> (1012, 'MATH', 'Lower', 22),
    -> (1013, 'ANTH', 'Upper', 27),
    -> (1014, 'ANTH', 'Upper', 21),
    -> (1015, 'ENGL', 'Lower', 25),
    -> (1016, 'ENGL', 'Upper', 32);
Query OK, 16 rows affected (0.00 sec)
Records: 16  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT Dept, Level, COUNT(*) AS NumberClasses
    -> FROM Classes
    -> GROUP BY Dept, Level WITH ROLLUP;
+------+-------+---------------+
| Dept | Level | NumberClasses |
+------+-------+---------------+
| ANTH | Upper |             5 |
| ANTH | Lower |             2 |
| ANTH | NULL  |             7 |
| ENGL | Upper |             3 |
| ENGL | Lower |             2 |
| ENGL | NULL  |             5 |
| MATH | Upper |             1 |
| MATH | Lower |             3 |
| MATH | NULL  |             4 |
| NULL | NULL  |            16 |
+------+-------+---------------+
10 rows in set (0.00 sec)

mysql>
mysql> drop table Classes;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Get the number of items for each type of wood
2.Get the number of woods for each type of item
3.Counting
4.How many times did drivers travel more than 200 miles in a day?
5.COUNT and GROUP BY
6.Count by group
7.COUNT(expr) doesn't count NULL values is useful when producing multiple counts from the same set of values.
8.The different forms of COUNT( ) can be very useful for counting missing values
9.Put the COUNT( ) expression in a HAVING clause instead.
10.Tracking Down Duplicates
11.Queries counting duplicate records have the following form
12.Counting and Identifying Duplicates
13.Count number of rows containing duplicated names
14.To see which names are duplicated in the cat_mailing table, use a summary query that displays the non-unique v
15.Eliminating Duplicates from a Query Result
16.Removing Duplicates of a Particular Row
17.Count duplicate records
18.How many days did Suzi drive?
19.How many messages were sent by each message sender
20.How many states did the United States consist of at the beginning of the 20th century?
21.Or to count weekend versus weekday trips
22.Count each value and see which one is most common
23.Generating Frequency Distributions
24.Relative frequency distributions
25.Counting Missing Values
26.Count them directly using SUM(ISNULL(score)).
27.Count the total number of rows
28.Categorizing Non-Categorical Data
29.Total number of the table vs unique values count
30.To produce a count-per-author summary that includes even authors with no books in the book table, use a LEFT J