sql server 2008 - Using "OR" versus "AND" Operator for Getting Specific Results -


will best explain problem here. ran query works checking people have completed either courseidnum=11 or courseidnum=12 in districtidnum=5, not both courseidnums. here script:

select max(p.firstname),    max(p.lastname),    max(p.email),    max(s.courseidnum) people p inner join registration r    on p.peopleid = r.peopleidnum inner join section s    on r.sectionidnum = s.sectionid inner join school sc    on p.schoolidnum = sc.schoolid s.courseidnum in (11, 12) , sc.districtidnum = 5 , r.completed='y' group p.peopleid having count(distinct s.courseidnum)=1 

now tasked retrieve data people in districtidnum=5 again, time check if have taken 3 courses, 2 out of 3 courses have same course name each of these 2 courses has 2 different courseidnums. so, edited where portion shown below. results returned people completed 12 , 177, there no result 11, 68 , 128. then, replaced or and operator, , 0 rows returned. in appreciated!

where s.courseidnum in (11, 12)    or s.courseidnum in (68, 177)    or s.courseidnum=128    , sc.districtidnum = 5    , r.completed='y' group p.peopleid having count(distinct s.courseidnum)=3 

here tables involved:

peopleid   firstname   lastname   email                     schoolidnum
1               esther         b                b@hotmail.com      33
2               tommy        l                 l@hotmail.com       55
3               liz               m                m@hotmail.com     90

registrationid   peopleidnum   sectionidnum
22                    1                      40
23                    2                      41
24                    3                      132
25                    1                      78
26                    2                      52
27                    1                      63

sectionid   courseidnum
40              11
41              12
52              68
63              128
78              177
132            195

courseid   coursename
11             health (old)
12             health (new)
68             pe (old)
128           keyboarding
177           pe (new)
195           computing

schoolid   districtidnum
33             5
55             5
90             12


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -