sql - Basic counting queries -
i have 2 tables class
, grade
.
this sample rows in class
table:
c_name....c_time...f_name..c_room...semester inss300...m5:30....dwight..219bc....fall11 inss300...t5:30....keen....216bc....fall11 mkt300....m5:30....lee.....112bc....sp11 mkt300....w5:30....lee.....112bc....sp11 inss421...t5:30....cory....212bc....fall11 fin300....th5:30...keen....219bc....fall11 inss300...w5:30....cory....219bc....fall11 inss300...f5:30....cory....216bc....fall11 inss422...f5:30....keen....219bc....fall11
and sample data in grade
table
s_name c_name g larry....inss300....b larry....fin300.....a sony.....inss300....c sony.....inss421....c gray.....inss300....c gray.....inss421....d trudy....inss300....b drum.....inss421....a drum.....fin300.....b puri.....inss422....c apple....inss422....c larry....inss422....c larry....inss300....b sony.....inss300....c gray.....inss300....c trudy....inss300....b
my question have answer is:
assume these tables in account ordb002. need give read permission on 3 tables group member sally account ordb009. assuming have given sally read permission on tables. sally wants count of grades (i.e., a, b, c etc..) faculty , course. write query sally count.
i have come query
select cla.f_name , gra.c_name , count(grade) , grade class1 cla , grade1 gra cla.c_name=gra.c_name group cla.f_name , gra.c_name , grade order cla.f_name , gra.c_name;
which gives me following output:
f_name c_name count(grade) g -------------------- ---------- ------------ - cory inss300 4 b cory inss300 4 c cory inss421 1 cory inss421 1 c cory inss421 1 d dwight inss300 2 b dwight inss300 2 c keen fin300 1 keen fin300 1 b keen inss300 2 b keen inss300 2 c keen inss422 3 c
i don't know why 4b , 4 c coming @ top of output when possibility can 2b , 2 c.
any suggestions?
it's because in class table have "duplicate" rows c_name , f_name, e.g.:
c_name c_time f_name c_room semester --------- -------- ------- ------- -------- inss300 w5:30 cory 219bc fall11 inss300 f5:30 cory 216bc fall11
each row in grade table inss300 going match against rows, resulting in double count.
so, if grade table doesn't note time class was, you're going have filter class table display 1 row per c_name , f_name.
alternatively, group rows in grade table first, , join them class table, if still needed display both rows (although don't think right approach problem you're trying solve).