|
Forums » .NET » SQL Server »
Posted Date: 19 Jun 2012 Posted By:: Somnath Member Level: Bronze Member Rank: 4421 Points: 4
Responses:
3
|
Hi I have 2 Tables First Table is
Primary -------------- PID | Name 1 Prime1 2 Prime2 3 Prime3 4 Prime4 5 Prime5
And Secound Table is
Mapping --------------- StudentId PID 1 2 2 2 3 2 1 3 2 3
I want to write the Query for to get only those StudentId Which has PID 2 AND 3 Result Should Be
StudentID ------------ 1 2
Please Help ASAP! THANKS In Advance!
|
Responses
|
#676214 Author: Rajalingam Member Level: Silver Member Rank: 618 Date: 19/Jun/2012 Rating:  Points: 2 | Hi Somnath,
use this query
select distinct s.studentid from firsttable f inner join secondtable s on s.pid=f.pid where f.pid in(2,3)
hope it will help you
| #676221 Author: Ultimaterengan Member Level: Gold Member Rank: 9 Date: 19/Jun/2012 Rating:  Points: 4 | To Create tables:-
create table [Primary](PID int,name varchar(max))
create table Mapping(StudentId int,PID Int)
To insert values to tables
Insert into [Primary] values(1,'Prime1') Insert into [Primary] values(2,'Prime2') Insert into [Primary] values(3,'Prime3') Insert into [Primary] values(4,'Prime4') Insert into [Primary] values(5,'Prime5')
Insert into Mapping values(1,2) Insert into Mapping values(2,2) Insert into Mapping values(3,2) Insert into Mapping values(1,3) Insert into Mapping values(2,3)
To Get your result:-
SELECT studentid , COUNT(studentid) AS NumberofTimes FROM Mapping GROUP BY studentid HAVING ( COUNT(studentid) > 1 )
Thanks & Regards G.Renganathan Nothing is mine ,Everything is yours!!! http://renganathan1984.blogspot.com/
| #676401 Author: kirthiga Member Level: Gold Member Rank: 219 Date: 20/Jun/2012 Rating:  Points: 2 | Hi Somnath,
Try this query
select StudentId from Mapping where PID=3 and StudentId in (select StudentId from Mapping where PID =2)
|
|
| Post Reply |
|
|
|
 | | This thread is locked for new responses. Please post your comments and questions as a separate thread. If required, refer to the URL of this page in your new post. |
|
|
|
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|