PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → WEBDEV19 - How to Code SQL, 2 files, only bring back Unique
WEBDEV19 - How to Code SQL, 2 files, only bring back Unique
Iniciado por guest, 22,jul. 2015 00:23 - 6 respuestas
Publicado el 22,julio 2015 - 00:23
Hi all -

I'm not very good at SQL, so rather than spend hours trying to figure this one out on my own, I figured I'd ask for help first.

I have 2 files, both have a ContactID field in them. I want to create a single list of ContactID's - without any duplicates in them. So basically if a contact shows up in either of these files, I want to work with them.

Any have some code to get me started? I don't know anything about inner / outer joins (other than they exist)
Publicado el 22,julio 2015 - 01:16
Out of the mind and not tested:

Select table1.customerID
UNION
Select table2.customerID

should do a distinct select

Select table1.customerID
UNION ALL
Select table2.customerID

should be with duplicates
Publicado el 22,julio 2015 - 01:17
Correction:

Select table1.customerID from table1
UNION
Select table2.customerID from table2
Publicado el 22,julio 2015 - 02:26
I'd suggest a slight change...

SELECT DISTINCT table1.customerID from table1
UNION
SELECT DISTINCT table2.customerID from table2
Publicado el 22,julio 2015 - 02:59
But if the same ContactID is in both files, after you do the union, wouldn't it show up twice in the final file?
Publicado el 22,julio 2015 - 11:11
Hi Joel,

No the ContactID wouldn't show up twice. This is the difference between UNION and UNION ALL.

UNION - removes duplicates
UNION ALL - keeps duplicates

Thanks
Ned!
Publicado el 22,julio 2015 - 14:26
Thanks guys!