| Author |
Topic |
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 11:34:13
|
| I have these as my star schema PhysicianPhysicianKey, NameDiseaseDiseaseKey, DiseaseName, DiseaseTypePatientPatientKey, Name, Gender, AgeDrugDrugKey, DrugNameTimeTimeKey, Date, DayOfWeek, Month, YearFactTableDiseaseKey, PatientKey, PhysicianKey, DrugKey, TimeKey, UnitCost, Quantity, TotalCostAssuming every key is a primary key in all the tables and is an integer.In the FactTable all the keys make composite key and other three are facts.Below Are the questions:: 1)Find the total sales of each drug.?2)How many patients were affected by lifestyle diseases in the age group of 20-35?3)Which drugs were most used for curing lifestyle diseases?4)Which physician was most consulted for genetic diseases?5)On which day of the week there is maximum demand of drugs?6)Which disease is most prominent in females?I was able to do first two...answer1) Select sum(TotalCost) from FactTable group by ProductKeyanswer2)select count(F.PatientKey) from FactTable F, Patient P, Disease D where P.PatientKey=F.PatientKey and D.DiseaseKey=F.DiseaseKey andD.DiseaseType='lifestyle' and(P.Age >20 and P.age < 35)Please help me answer the other four....Dont think I am lazy I just am not able to do them....please help ...please |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 11:42:32
|
| i will give you hint3, most used drug means count of used quantity of drug is maximum in fact4, most consulted physician is one with maximum occurance in fact5, look for day in time dimension with maximum occurance in fact6, look for disease which occur most in fact for gender = 'female'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 12:01:32
|
| 3, most used drug means count of used quantity of drug is maximum in factI can get the count of most sold drug but how can i get the name check the following query..select max(sum(F.Quantity)) from FactTable F, Disease D where F.DiseaseKey=D.DiseaseKey andD.DiseaseType='lifestyle'Group By DiseaseKeyThe above query will return me the quantity of the max sold drug fro curing lifestyle diseases... but how can i get the name...I am stuck here only... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:06:12
|
| join this with drug table on drugkey to get other associated info------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 12:19:07
|
quote: Originally posted by visakh16 join this with drug table on drugkey to get other associated info------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
How can I join this..please help me with this...How can I join...please atleast provide the exact query for this one...I will try others....please |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:23:47
|
| [code]select TOP 1 DR.DrugName from FactTable F, Disease D ,Drug DRwhere F.DiseaseKey=D.DiseaseKey andD.DiseaseType='lifestyle' andDR.DrugKey = F.DrugKeyGroup By DrugNameORDER BY sum(F.Quantity) DESC[/code]add WITH TIES if you need to return all drug in case of tie for max usage------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 12:29:36
|
quote: Originally posted by visakh16
select TOP 1 DR.DrugName from FactTable F, Disease D ,Drug DRwhere F.DiseaseKey=D.DiseaseKey andD.DiseaseType='lifestyle' andDR.DrugKey = F.DrugKeyGroup By DrugNameORDER BY sum(F.Quantity) DESC add WITH TIES if you need to return all drug in case of tie for max usage------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks a Lot ... now i will try all other queries and let u know... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:31:46
|
| cool...all the best------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 12:51:20
|
| U gave this answer for question 3select TOP 1 DR.DrugName from FactTable F, Disease D ,Drug DRwhere F.DiseaseKey=D.DiseaseKey andD.DiseaseType='lifestyle' andDR.DrugKey = F.DrugKeyGroup By DrugNameORDER BY sum(F.Quantity) DESCInstead Of Group By DrugName shouldn't it be Group By DR.DrugName(please do tell)Please check my below answers ...please do tell are they correct....(Literally thanks a lot for helping...)answer4)select TOP 1 PH.Name from Facttable F, Disease D, Physician PHwhereF.DiseaseKey=D.DiseaseKey andF.PhysicianKey=PH.PhysicianKey andD.DiseaseType='genetic' Group By PH.NameOrder By count(F.PhysicianKey) DESCanswer5)select TOP 1 T.Day from FactTable F, Time T where F.TimeKey=T.TimeKEy andGroup By T.DayOrder By count(F.TimeKey) DESCanswer6)select TOP 1 D.DiseaseName from FactTable F, Patient P, Disease DwhereF.DiseaseKey = D.DiseaseKEy andF.PatientKey=P.PatientKey andP.Gender='female'Group By D.DiseaseNameOrder By count(DiseaseKey) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:57:21
|
quote: Originally posted by vijayrawatsan U gave this answer for question 3select TOP 1 DR.DrugName from FactTable F, Disease D ,Drug DRwhere F.DiseaseKey=D.DiseaseKey andD.DiseaseType='lifestyle' andDR.DrugKey = F.DrugKeyGroup By DrugNameORDER BY sum(F.Quantity) DESCInstead Of Group By DrugName shouldn't it be Group By DR.DrugName(please do tell)its not a problem as you've only single occurance of DrugName anyways its always best to use alias for clarityPlease check my below answers ...please do tell are they correct....(Literally thanks a lot for helping...)answer4)select TOP 1 PH.Name from Facttable F, Disease D, Physician PHwhereF.DiseaseKey=D.DiseaseKey andF.PhysicianKey=PH.PhysicianKey andD.DiseaseType='genetic' Group By PH.NameOrder By count(F.PhysicianKey) DESCcorrectanswer5)select TOP 1 T.Day from FactTable F, Time T where F.TimeKey=T.TimeKEy andGroup By T.DayOrder By count(F.TimeKey) DESCnot correct this will just give day occuring maximum times in fact but you need also consider condition there is maximum demand of drugs so need a join to drug as well to get maximum count and get day corresponding to thatanswer6)select TOP 1 D.DiseaseName from FactTable F, Patient P, Disease DwhereF.DiseaseKey = D.DiseaseKEy andF.PatientKey=P.PatientKey andP.Gender='female'Group By D.DiseaseNameOrder By count(DiseaseKey)need a desc in order by as you want to look in decending order
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijayrawatsan
Starting Member
18 Posts |
Posted - 2010-04-27 : 13:12:30
|
| Thank You Thank YOu Thank YOuvery very very much for such fast replies...Thanks a LOT. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 13:47:09
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|