Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
need to pick data from 2 tables.


The tables are 1. COMPLAINT (EMPNO,EMPNAME)
2. UTILITY (EMPNO,EMPNAME)

The query should exclude records in UTILITY which are in COMPLAINT

The field EMPNO is the main reference


SELECT TOP 1 EMPNO ,
EMPNAME FROM COMPLAINT
GROUP BY EMPNO,EMPNAME ORDER BY EMPNO DESC
UNION
SELECT EMPNO,EMPNAME FROM UTILIY
AND UTILITY.EMPNO
NOT IN COMPLAINT


Please assist

Thanks

What I have tried:

Checked my earlier codes in CTE .

Have also checked the Internet. However I never found any example with the 'NOT IN'

IN CTE.

Any referencesites will be most welcomed
Posted
Updated 5-Oct-16 23:46pm
v5
Comments
Member 12770648 6-Oct-16 5:43am    
I need an example in CTE that has NOT IN and UNION such that EMPNO in UTILITY that are
also in COMPLAINT are excluded


UTILITY (EMPNO,EMPNAME)
EMPNO EMPNAME
001 IVY
002 HAN
003 AMY
004 SAM
005 DAN


COMPLAINT (EMPNO,EMPNAME)
EMPNO EMPNAME
001 IVY
002 HAN
003 AMY

Thanks

1 solution

Without knowing your tables and exactly what you want to return it's not possible to tell you exactly what to do. But have a look here: Visual Representation of SQL Joins[^] - you probably want one of the three EXCLUDING JOINs, and it gives sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900