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

I have my sq l table in the example format below:

id      Stud          Amount    Sem

2       007           5600      SemI
3       002           4500      Sem1 
4       001           5600      SemI
5       007           4500      Sem1I
6       001           5600      SemII
7       002           4500      Sem1
8       002           4500      Sem1I
9       005           4500      SemII



How can i write an Sql query to get only those students who have made payments in semII only and NOT in semI.
E.g. Student 005

Thanks for your support

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 16-Feb-14 3:44am
v2

SQL
SELECT t1.stud FROM table1 t1 WHERE  t1.sem='semII' AND  NOT EXISTS
(SELECT * FROM table1 t2 WHERE t2.sem='semI' AND t1.stud=t2.stud)
 
Share this answer
 
v2
Try:
SQL
SELECT Stud FROM MyTable a
JOIN (SELECT stud, COUNT(sem) AS sems
      FROM MyTable
      WHERE Sem='SemI' OR Sem='SemII'
      GROUP BY Stud) b
ON a.Stud = b.Stud
WHERE b.sems=1 AND a.Sem='SEMII'
 
Share this answer
 
hi you can do it...with the help of where condition...

Where Condition is used like that a bool condition. for example is
SQL
select count(*) from TableName where columnname="Condition Value"


if the above query is return 1 then where condition is true other wise condition is false.
try this
SQL
Select * from Student where sem='SemII'


for any query hit to reply..
 
Share this answer
 
Comments
OriginalGriff 16-Feb-14 10:41am    
Reason for my vote of one: Please read the question carefully before posting a solution.
The OP wants details based on data from two rows, which a simple WHERE condition can't do: it only looks at each row in isolation.
SQL
SELECT * FROM table1 WHERE  sem='semII' AND sem NOT IN
(SELECT sem FROM table1 WHERE sem='semI')
 
Share this answer
 
v2
SQL
SELECT [current].rowInt, [current].Value, ISNULL([next].Value, 0) - [current].Value
FROM sourceTable AS [current]
LEFT JOIN sourceTable AS [next]
      ON [next].rowInt = (SELECT MIN(rowInt) FROM sourceTable WHERE rowInt > [current].rowInt)
 
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