Click here to Skip to main content
15,888,802 members
Home / Discussions / Database
   

Database

 
GeneralRe: whats the proper defination of view Pin
Yulianto.9-Mar-05 14:32
Yulianto.9-Mar-05 14:32 
GeneralRe: whats the proper defination of view Pin
Colin Angus Mackay9-Mar-05 14:53
Colin Angus Mackay9-Mar-05 14:53 
Generalit is givi ng me syntax error Pin
feeha9-Mar-05 7:37
feeha9-Mar-05 7:37 
GeneralRe: it is givi ng me syntax error Pin
ToddHileHoffer9-Mar-05 8:32
ToddHileHoffer9-Mar-05 8:32 
GeneralRe: it is givi ng me syntax error Pin
feeha9-Mar-05 9:26
feeha9-Mar-05 9:26 
GeneralQuestion on UNION syntax Pin
KreativeKai9-Mar-05 6:06
professionalKreativeKai9-Mar-05 6:06 
GeneralRe: Question on UNION syntax Pin
ToddHileHoffer9-Mar-05 8:35
ToddHileHoffer9-Mar-05 8:35 
GeneralRe: Question on UNION syntax Pin
Edbert P9-Mar-05 13:11
Edbert P9-Mar-05 13:11 
UNION is used to combine results from two or more selects. You cannot use it to remove duplicates (although if you specify SELECT DISTINCT it will remove duplicate records).

From your example it seems that you want to show all employees from both tables, but only one distinct employee name for each EmpID.
You may want to use one table as the primary table (to get only one name) and get the rest of the employees from the secondary table.
Here's how you can return all employees from both tables:
SELECT LastName, FirstName, EmpNo AS EmpID
FROM table1
WHERE EmpTermDate IS NULL
UNION
SELECT LastName, FirstName, SocSecNo AS EmpID
FROM table2
<code>AND SocSecNo NOT IN (SELECT EmpNo FROM table1)</code>
ORDER BY LastName ASC, FirstName ASC, EmpID ASC

If both tables contain the same number of employee id's (i.e. they all match), then you can use INNER JOIN and get the name from one table only.
SELECT table1.LastName, table1.FirstName, EmpNo
FROM table1
INNER JOIN table2 ON table1.EmpNo = table2.SocSecNo
WHERE table1.EmpTermDate IS NULL
ORDER BY LastName ASC, FirstName ASC, EmpID ASC


There are other ways to return just one name from both tables, but this one works and it returns all employees from both tables.
Hope it helps Smile | :)
GeneralRe: Question on UNION syntax Pin
KreativeKai10-Mar-05 4:25
professionalKreativeKai10-Mar-05 4:25 
GeneralRe: Question on UNION syntax Pin
Edbert P10-Mar-05 15:36
Edbert P10-Mar-05 15:36 
GeneralRe: Question on UNION syntax Pin
KreativeKai11-Mar-05 3:15
professionalKreativeKai11-Mar-05 3:15 
GeneralTrancation Replication Problems Pin
tyaramis9-Mar-05 0:19
tyaramis9-Mar-05 0:19 
GeneralDBF Export from SQL server Pin
davethiss9-Mar-05 0:06
davethiss9-Mar-05 0:06 
GeneralHow Pin
Yulianto.8-Mar-05 17:46
Yulianto.8-Mar-05 17:46 
GeneralRe: How Pin
Yulianto.8-Mar-05 18:07
Yulianto.8-Mar-05 18:07 
GeneralRe: How Pin
ToddHileHoffer9-Mar-05 8:38
ToddHileHoffer9-Mar-05 8:38 
Questionhow to create a view in sql Pin
feeha8-Mar-05 9:10
feeha8-Mar-05 9:10 
AnswerRe: how to create a view in sql Pin
Chris Meech8-Mar-05 9:34
Chris Meech8-Mar-05 9:34 
AnswerRe: how to create a view in sql Pin
Anonymous10-Mar-05 0:26
Anonymous10-Mar-05 0:26 
Questionhow to save pic Pin
javad_20058-Mar-05 7:05
javad_20058-Mar-05 7:05 
AnswerRe: how to save pic Pin
ToddHileHoffer8-Mar-05 7:28
ToddHileHoffer8-Mar-05 7:28 
GeneralNeed a Help Pin
Anonymous8-Mar-05 5:42
Anonymous8-Mar-05 5:42 
GeneralRe: Need a Help Pin
Chris Meech8-Mar-05 5:51
Chris Meech8-Mar-05 5:51 
GeneralRe: Need a Help Pin
Anonymous8-Mar-05 11:57
Anonymous8-Mar-05 11:57 
GeneralRe: Need a Help Pin
Shawn200014-Mar-05 4:45
Shawn200014-Mar-05 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.