Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServer Writing union in a single statement

I have query as given below:



SELECT * FROM [PMDB_DEV].[dbo].[TRSRCFIN] 
WHERE proj_id = 167592 
AND taskrsrc_id NOT IN
(
SELECT taskrsrc_id FROM [PMDB_ARC].[dbo].[TRSRCFIN_TEST]
WHERE proj_id = 167592 
)
UNION
SELECT * FROM [PMDB_DEV].[dbo].[TRSRCFIN] 
WHERE proj_id = 167592 
AND fin_dates_id NOT IN
(
SELECT fin_dates_id FROM [PMDB_ARC].[dbo].[TRSRCFIN_TEST]
WHERE proj_id = 167592 
)

Basically the query returns all the rows where either taskrsrc_id or fin_dates_id should not present in the subquery data.

Can I do this without using UNION??

Thanks, Mahesh

like image 859
Mahesh Avatar asked Jan 20 '26 22:01

Mahesh


1 Answers

select  * 
from    [PMDB_DEV].[dbo].[TRSRCFIN] t1
where   proj_id = 167592 
        and not exists
        (
        select  *
        from    [PMDB_ARC].[dbo].[TRSRCFIN_TEST] t2
        where   t2.proj_id = t1.proj_id
                and (
                    t1.taskrsrc_id = t2.taskrsrc_id
                    or
                    t1.fin_dates_id = t2.fin_dates_id
                )
        )
like image 189
Andomar Avatar answered Jan 23 '26 21:01

Andomar