cancel
Showing results for 
Search instead for 
Did you mean: 

distinct over several columns

Baron
Participant
784

Is it possible to select 2 columns distinctly in one single select statement?

In this example I have the problem that 'Michael' will be listed twice:

create or replace table employees (firstname varchar(100), lastname varchar(100), phone varchar(100));
insert into employees values 
('Michael', 'Carthy', '01123456'),
('John', 'Carther', '01654321'),
('Raphael', 'Michael', '01654123');
select list (distinct firstname) || ',' ||  list (distinct lastname) from employees

One way to solve the problem is something like this (approach B):

select list(distinct f1) from
(select firstname f1 from employees 
union 
select lastname f1 from employees) T1

But I want to avoid the second select statement, since the table employees enough big is.

Accepted Solutions (1)

Accepted Solutions (1)

chris_keating
Product and Topic Expert
Product and Topic Expert

DISTINCT removes duplicate rows from a result set but does not operate at a individual column level in a result set. The UNION is a reasonable solution based on the requirement.

Answers (0)