Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have two table Jobs(name,job1,job2) and Users(name), while i m using where cluase getting this records

1.

Name job1 job2
A 5 88
B 8 4
C 6 10
Where Dep='Op'
2.

Name job1 job2
A 1 20
B 3 25
C 5 11
Where Dep='SA'
But i want single fetchable data like below after applying both where Conditions

Name job1 job2 job1 job2
A 5 88 1 20
B 8 4 3 25
C 6 10 5 11


Any suggestion or Help.
Posted

As seen from the question two tables are given,
Jobs(name,job1,job2) and Users(name). But in the results shown in the question, the Where Dep='Op' is given, from which it appears that there is one more column Dep in the Jobs table. Further the Users table is not used for the results shown.

If there is a column Dep in the Jobs table then the following sql statement can be used
SQL
SELECT Jobs1.Name, Jobs1.Job1, Jobs1.Job2, Jobs2.Job1, Jobs2.Job2
FROM
    (SELECT Name, Job1, Job2 
     FROM Jobs
     WHERE Dep = 'Op')  Jobs1
JOIN
    (SELECT Name, Job1, Job2
     FROM Jobs
     WHERE Dep = 'SA') Jobs2
ON Jobs1.Name = Jobs2.Name


[Edit] From the comment given to the Solution 1, it is seen that Dep is available in Users Table. Accordingly the Query is modified [/Edit]
SQL
SELECT Jobs1.Name, Jobs1.Job1, Jobs1.Job2, Jobs2.Job1, Jobs2.Job2
FROM
    (SELECT Jobs.Name, Job1, Job2
     FROM Jobs
     JOIN Users ON Jobs.Name = Users.Name
     WHERE Users.Dep = 'Op')  Jobs1
JOIN
    (SELECT Jobs.Name, Job1, Job2
     FROM Jobs
     JOIN Users ON Jobs.Name = Users.Name
     WHERE Users.Dep = 'SA') Jobs2
ON Jobs1.Name = Jobs2.Name
 
Share this answer
 
v2
Comments
Sandeep Mewara 5-May-12 14:33pm    
5 for the effort & update.
VJ Reddy 5-May-12 20:40pm    
Thank you, Sandeep.
SQL
select a.name ,a.job1,a.job2,b.job1,b.job from OP  a join SA b where a.Name=b.Name
 
Share this answer
 
v2
Comments
SDAP_INDIA 5-May-12 8:03am    
i m using

1. select a.name,b.job1,b.job2 from Jobs b inner join Users a on a.name=b.name where b.dep='OP'

2. select a.name,b.job1,b.job2 from Jobs b inner join Users a on a.name=b.name where b.dep='SA'

I want result of both query result like below in a single selection query.

Name job1 job2 job1 job2
A 5 88 1 20
B 8 4 3 25
C 6 10 5 11
Use the following query.......


SQL
select a.name ,a.job1,a.job2,b.job1,b.job2 from Jobs  a join users b where a.Name=b.Name where a.Dep In('OP','SA') OR b.Dep in ('OP','SA')



Best of luck........

Hope it helps.....
 
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