| Author |
Topic |
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-04-09 : 00:40:01
|
| Dear Friends,I need small help.I have 3 tables Grades,CollectionTest and Oraganization.Taking Collection_Test into consideration, i need display invalid records from it.ForeignKeys---------------collection_test(facilitycode) refers to Grades(orgid)and one more thing is that there is no relationship between collection_test and Organization tablesbut logically collection_test(facilitycode) refers to Oraganization(orgid).Condition 1 : Take each record from "Collection_test" table,consider facilitycode,gradeid fields.For that facility code u need to take corressponding gradeid and lookup into "Grades" table and check for that facilitycode/orgid , the grade is falling within the range or not.Example, if i consider 1 st record in Collection_test table 430 is my faciltycode and 5 is my gradeid.Now i need to lookup into Grades table for orgid 430 and get the corresponding gradeids from the "Grades" table.(i.e for 430 i will get grades from 1,2,3,4,5,6,7,8,9,10) and check whether collection_test(gradeid) i.e 5 is falling within1,2,3,4,5,6,7,8,9,10 range or not. If not it is an invalid record and i need to display it.Again we have an Exception here, Eventhough the grade is not falling within the range but taking into "Organization" table into consideration ( this is second level condition checking) check for the orgid in this table( collection_test(facilitycode) refers to Oraganization(orgid)) and for that organization id, if the institution code (instid)is IN (21,41,61,82) then i can ignore that record i.e it is no more an INVALID record and is not displayed in the output.So as a result if u take 2nd record into consideration, i.e 5 431.Though it fails in the first condition but it pass's in thesecond level conditon and finally no rows is selected for the output.I tried till the first level of the condition , but got stuck for the second level condition.Please help me out.Thanks in Advance. create table Grades(orgid int, gradeid int)insert into grades select 430,1union allselect 430,2union allselect 430,3union allselect 430,4union allselect 430,5union allselect 430,6union allselect 430,7union allselect 430,8union allselect 430,9union allselect 430,10union allselect 431,11union allselect 431,11create table collection_test(CoolectionID int, dmid int, gradeid int, facilitycode int)insert into collection_test select 5,50,5,430union allselect 5,50,5,431create table organization(orgid int, instid int, district int)insert into organizationselect 430,21,799union allselect 430,41,800union allselect 430,61,400union allselect 430,82,500union allselect 430,11,900 union allselect 431,21,500union allselect 431,41,500SELECT a.CoolectionID,a.dmid,a.gradeid,a.facilitycodeFROM collection_test awhere not exists (select 1 from Grades where orgid=a.facilitycodeand a.gradeid in (select gradeid from grades where orgid = a.facilitycode )) |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-04-09 : 02:47:03
|
| But in the first level how do you define "range".For example in table grades for orgid 431 the gradeid is 10 & 15 but in collection test the gradeid is 12 for facility code 431.The should 431 should also be selected? |
 |
|
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2009-04-09 : 02:56:21
|
Hi, try thisSELECT a.CoolectionID,a.dmid,a.gradeid,a.facilitycode,o.instidFROM collection_test a,organization o,grades gwhere a.gradeid=g.gradeid and o.orgid=g.orgid andnot exists (select 1 from Grades where orgid=a.facilitycodeand a.gradeid in (select gradeid from grades where orgid = a.facilitycode ))and o.instid not in ('21','41','61','82')group by a.CoolectionID,a.dmid,a.gradeid,a.facilitycode,o.instidKunal |
 |
|
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-04-09 : 05:46:26
|
| Hey Guys,I got the answer. This is the correct one.SELECTa.CoolectionID,a.dmid,a.gradeid,a.facilitycodeFROM collection_test awherenot exists (select 1 from Grades where orgid=a.facilitycodeand a.gradeid in (select gradeid from grades where orgid = a.facilitycode ))and not exists (select 1 from organization c where c.orgid = a.facilitycode and instid IN (21,41,61,82))Thanks so much! |
 |
|
|
|
|
|