DB2 - Exemplos simples - DISTINCT



Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

DB2 - Exemplos simples - DISTINCT

SQL uses DISTINCT to remove duplicate rows from the result set.
O SQL usa DISTINCT para remover linhas duplicadas do conjunto de resultados.

Syntax - Sintaxe:

    SELECT DISTINCT column1, column2, ...
      FROM table_name;

DB2 Database - Banco de dados DB2:

Below is a selection from the "Product" table in the DB2 database.
Abaixo está uma seleção da tabela "Produto" no banco de dados DB2.

ProductID ProductDesc Category SRP QtyOnHand TotalValue
7001 Mouse Accessories 75.00
7002 Harddrive 65.00 20 1,300
7003 Keyboard Accessories 36.00 33 1,118.00
7004 RAM Components 23.50 16 376.00
7005 Honda Bikes 1,200
7006 PEN 7.45 10 74.50
7007 Keyboard Accessories 40.00 33 1,118.00
7008 RAM Components 23.50 16 376.00

Example 1 - Exemplo 1:

For getting the unique ProductDesc, you need to use the following query.
A instrução SQL acima lista o número de ProductDesc diferentes (distintos) na tabela "Produto".

    SELECT DISTINCT ProductDesc FROM Product;

The above SQL statement lists the number of different (distinct) ProductDesc in the "Product" table.
A instrução SQL acima lista o número de ProductDesc diferentes (distintos) na tabela "Produto".

Result - Resultado:

ProductIDProductDescCategorySRPQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7005HondaBikes1,200
7006PEN7.451074.50

Example 2 - Exemplo 2:

Assume that T1 is a single-column table with three rows.
Column C1 has the following values:
1, 2, and null. Consider the following query:

Suponha que T1 seja uma tabela de coluna única com três linhas.
A coluna C1 possui os seguintes valores:
1, 2 e nulo. Considere a seguinte consulta:

    SELECT * 
      FROM T1
     WHERE C1 IS DISTINCT FROM :HV;

The following table shows the value of the predicate for various values of C1 and the host variable.
A tabela a seguir mostra o valor do predicado para vários valores de C1 e a variável do host.

Value of C1 Value of HV Result of predicate
1 2 True
2 2 False
null 2 True
1 null True
2 null True
null null False

Example 3 - Exemplo 3:

Assume the same table as in the first example, but now consider the negative form of the predicate in the query:
Suponha a mesma tabela do primeiro exemplo, mas agora considere a forma negativa do predicado na consulta:

    SELECT * 
      FROM T1
     WHERE C1 IS NOT DISTINCT FROM :HV;

The following table shows the value of the predicate for various values of C1 and the host variable.
A tabela a seguir mostra o valor do predicado para vários valores de C1 e a variável do host.

Value of C1 Value of HV Result of predicate
1 2 False
2 2 True
null 2 False
1 null False
2 null False
null null True