Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have 2 Queries

SQL
select Users.UserId,Users.EmpNo,EmpPayments.SalBasic from Users join EmpPayments on Users.UserId=EmpPayments.UserId where EmpPayments.MonthId=6 and EmpPayments.YearId=2010 and Users.MainContractorSw='Y' and Users.UserId=43

select Users.UserId,Users.EmpNo,EmpPayments.SalBasic from Users join EmpPayments on Users.UserId=EmpPayments.UserId where EmpPayments.MonthId=6 and EmpPayments.YearId=2010 and Users.MainContractorId=43



Now I want to use these two Queries as one query and the result should be Combined of the Individual Queries
Posted

Hi Vignesh,

Please use UNION to combine both result like:

SQL
select Users.UserId,Users.EmpNo,EmpPayments.SalBasic from Users join EmpPayments on Users.UserId=EmpPayments.UserId where EmpPayments.MonthId=6 and EmpPayments.YearId=2010 and Users.MainContractorSw='Y' and Users.UserId=43
UNION
select Users.UserId,Users.EmpNo,EmpPayments.SalBasic from Users join EmpPayments on Users.UserId=EmpPayments.UserId where EmpPayments.MonthId=6 and EmpPayments.YearId=2010 and Users.MainContractorId=43



Please do let me know, if you have any doubt.

Please provide "Vote":thumbsdown: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
Comments
Bassam Abdul-Baki 18-Nov-10 9:23am    
I'd suggest changing your thumbs down icon to the thumbs up one for helpful replies.

Please provide "Vote" Thumbs Down if this would be helpful....
thatraja 18-Nov-10 10:37am    
Good catch Bassam :-)
So instead of "Users.MainContractorSw='Y' and Users.UserId=43" and "Users.MainContractorId=43" you want "(Users.MainContractorSw='Y' and Users.UserId=43) or (Users.MainContractorId=43)".
 
Share this answer
 
Comments
Sunasara Imdadhusen 18-Nov-10 9:17am    
Good one
Sunasara Imdadhusen 18-Nov-10 9:17am    
This also can give same result
Sunasara Imdadhusen 18-Nov-10 9:25am    
Ok. can you tell me what it will return what should be?
Vigneshb6 18-Nov-10 9:27am    
First Query Results Main Contractor Results and his salary
and the second query should return the SubContractors Results and there salaries of that particular Main Contractor. If i use the Union i will get the results. but if there are no MainContractors details in the EmpPayments table it should not display the SubContractor's Details actually i have to use this query in Crystal Report.
Vigneshb6 18-Nov-10 9:31am    
Sorry i can't use intersect here

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