Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yogeshwari_am
Starting Member
1 Post |
Posted - 2012-03-08 : 07:10:08
|
Hi, I have a Payment table,in that I am storing StudID,amount and month.For the range of months like from month 3 to 6,I would like to check that StudID has made the payment for all the months in that range.if i writeselect * from dbo.tbl_MonthlyPayment where month between 6 and 7it also returns if the Student paid for only 6th monthCan anyone tell me how to do..thanks in advance |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-08 : 07:38:47
|
[code]-- sample tabledeclare @sample table (StudId int, [month] int)-- sample datainsert @sampleselect 1, 6 union allselect 1, 7 union allselect 2, 5 union allselect 2, 6 union allselect 2, 7 union allselect 2, 8 union allselect 3, 6 union allselect 4, 7-- show sample dataselect * from @sample-- solution1select distinct * from(selectStudId,count(*) over (partition by StudId) as cntfrom @samplewhere [month] in (6,7))dtwhere cnt=2-- solution2select StudIdfrom @samplegroup by StudIdhaving sum(case when [month] in (6,7) then 1 else 0 end)=2[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 10:40:48
|
this will give all students who has done payments on all months in range 3 to 6select StudIdfrom @samplewhere [month] between 3 and 6group by StudIdhaving count(distinct month) = 3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|