The fraction of the records that contain unique or non-unique names: : Distinct « Select Clause « SQL / MySQL






The fraction of the records that contain unique or non-unique names:

       
mysql>
mysql> CREATE TABLE cat_mailing
    -> (
    ->  last_name       CHAR(40) NOT NULL,
    ->  first_name      CHAR(40) NOT NULL,
    ->  street          CHAR(40) NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO cat_mailing (first_name, last_name, street)
    ->  VALUES
    ->          ('Jim','Isaacson','515 Fordam St., Apt. 917'),
    ->          ('Wallace','Baxter','57 3rd Ave.'),
    ->          ('Taylor','McTavish','432 River Run'),
    ->          ('Marlene','Pinter','9 Sunset Trail'),
    ->          ('WALLACE','BAXTER','57 3rd Ave.'),
    ->          ('Bartholomew','Brown','432 River Run'),
    ->          ('Marlene','Pinter','9 Sunset Trail'),
    ->          ('Wallace','Baxter','57 3rd Ave., Apt 102')
    -> ;
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM cat_mailing;
+-----------+-------------+--------------------------+
| last_name | first_name  | street                   |
+-----------+-------------+--------------------------+
| Isaacson  | Jim         | 515 Fordam St., Apt. 917 |
| Baxter    | Wallace     | 57 3rd Ave.              |
| McTavish  | Taylor      | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
| BAXTER    | WALLACE     | 57 3rd Ave.              |
| Brown     | Bartholomew | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
| Baxter    | Wallace     | 57 3rd Ave., Apt 102     |
+-----------+-------------+--------------------------+
8 rows in set (0.00 sec)

mysql>
mysql>
mysql> SELECT COUNT(DISTINCT last_name, first_name) / COUNT(*)
    -> AS 'unique',
    -> 1 - (COUNT(DISTINCT last_name, first_name) / COUNT(*))
    -> AS 'non-unique'
    -> FROM cat_mailing;
+--------+------------+
| unique | non-unique |
+--------+------------+
| 0.6250 |     0.3750 |
+--------+------------+
1 row in set (0.00 sec)

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

mysql>

   
    
    
    
    
    
    
  








Related examples in the same category

1.Use DISTINCT to get non-dupliate records
2.Use DISTICNT to get unique value
3.Eliminating Duplicate Data Using DISTINCT 1
4.Eliminating Duplicate Data Using DISTINCT 2
5.Select distinct records using JOIN
6.The SQL key word DISTINCT has the effect that equivalent data records are output only once.
7.DISTINCT works with multiple-column output too.
8.how many different drivers there are, use COUNT(DISTINCT)
9.DISTINCT with two columns
10.DISTINCT works with expressions, not just column values.
11.Inserting name values into the multisequence table generates separate sequences for each distinct name:
12.Count distinct
13.Distinct sub string
14.Average distinct value
15.Return a list of surnames, with each surname appearing only once?