Useful shortcuts for vi editor

Showing posts with label distinct. Show all posts
Showing posts with label distinct. Show all posts

Thursday 19 April 2018

e.g. count and distinct aggregate functions

examples; count (1) - count(*) - count(column_name) - count(distinct (column_name))

e.g. count aggregate function
SQL>
SELECT
    COUNT(*)
FROM
    employees;

  COUNT(*)
----------
       107

SQL>
SELECT
    COUNT(1)
FROM
    employees;

  COUNT(*)
----------
       107

SQL>
SELECT
    COUNT(commission_pct)
FROM
    employees;

COUNT(COMMISSION_PCT)
---------------------
                   35

Info: COUNT(1) and COUNT(*) calculate all rows even all columns data are NULL. However, COUNT(COLUMN_NAME) ignores NULL data.

e.g. distinct aggregate function
SQL>
SELECT
    distinct(commission_pct)
FROM
    employees;

COMMISSION_PCT
--------------
          NULL
           ,15
           ,35
            ,4
            ,3
            ,2
           ,25
            ,1

8 rows selected.

Info: DISTINCT(COLUMN_NAME) function uses not to see duplicated rows and also it shows NULL column if it exists.

e.g. both of count and distinct aggregate functions
SQL>
SELECT
    COUNT(DISTINCT(commıssıon_pct) )
FROM
    employees;

COUNT(DISTINCT(COMMISSION_PCT))
-------------------------------
                              7

Info: count(column_name) ignores NULL values, even if distinct function is used.