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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Join and group by problem(need help with query)

Author  Topic 

vijayrawatsan
Starting Member

18 Posts

Posted - 2010-04-27 : 11:34:13
I have these as my star schema

Physician
PhysicianKey, Name

Disease
DiseaseKey, DiseaseName, DiseaseType

Patient
PatientKey, Name, Gender, Age

Drug
DrugKey, DrugName

Time
TimeKey, Date, DayOfWeek, Month, Year

FactTable
DiseaseKey, PatientKey, PhysicianKey, DrugKey, TimeKey, UnitCost, Quantity, TotalCost


Assuming 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 ProductKey

answer2)
select count(F.PatientKey) from FactTable F, Patient P, Disease D where
P.PatientKey=F.PatientKey and
D.DiseaseKey=F.DiseaseKey and
D.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 hint

3, most used drug means count of used quantity of drug is maximum in fact
4, most consulted physician is one with maximum occurance in fact
5, look for day in time dimension with maximum occurance in fact
6, look for disease which occur most in fact for gender = 'female'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 fact

I 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 and
D.DiseaseType='lifestyle'
Group By DiseaseKey


The 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...
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 MVP
http://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
Go to Top of Page

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 DR
where
F.DiseaseKey=D.DiseaseKey and
D.DiseaseType='lifestyle' and
DR.DrugKey = F.DrugKey
Group By DrugName
ORDER 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 DR
where
F.DiseaseKey=D.DiseaseKey and
D.DiseaseType='lifestyle' and
DR.DrugKey = F.DrugKey
Group By DrugName
ORDER BY sum(F.Quantity) DESC


add WITH TIES if you need to return all drug in case of tie for max usage

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Thanks a Lot ... now i will try all other queries and let u know...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 12:31:46
cool...all the best

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vijayrawatsan
Starting Member

18 Posts

Posted - 2010-04-27 : 12:51:20
U gave this answer for question 3

select TOP 1 DR.DrugName
from FactTable F, Disease D ,Drug DR
where
F.DiseaseKey=D.DiseaseKey and
D.DiseaseType='lifestyle' and
DR.DrugKey = F.DrugKey
Group By DrugName
ORDER BY sum(F.Quantity) DESC

Instead 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 PH
where
F.DiseaseKey=D.DiseaseKey and
F.PhysicianKey=PH.PhysicianKey and
D.DiseaseType='genetic'
Group By PH.Name
Order By count(F.PhysicianKey) DESC


answer5)
select TOP 1 T.Day from FactTable F, Time T
where
F.TimeKey=T.TimeKEy and
Group By T.Day
Order By count(F.TimeKey) DESC


answer6)
select TOP 1 D.DiseaseName from FactTable F, Patient P, Disease D
where
F.DiseaseKey = D.DiseaseKEy and
F.PatientKey=P.PatientKey and
P.Gender='female'
Group By D.DiseaseName
Order By count(DiseaseKey)
Go to Top of Page

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 3

select TOP 1 DR.DrugName
from FactTable F, Disease D ,Drug DR
where
F.DiseaseKey=D.DiseaseKey and
D.DiseaseType='lifestyle' and
DR.DrugKey = F.DrugKey
Group By DrugName
ORDER BY sum(F.Quantity) DESC

Instead 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 clarity



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 PH
where
F.DiseaseKey=D.DiseaseKey and
F.PhysicianKey=PH.PhysicianKey and
D.DiseaseType='genetic'
Group By PH.Name
Order By count(F.PhysicianKey) DESC

correct


answer5)
select TOP 1 T.Day from FactTable F, Time T
where
F.TimeKey=T.TimeKEy and
Group By T.Day
Order By count(F.TimeKey) DESC
not 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 that


answer6)
select TOP 1 D.DiseaseName from FactTable F, Patient P, Disease D
where
F.DiseaseKey = D.DiseaseKEy and
F.PatientKey=P.PatientKey and
P.Gender='female'
Group By D.DiseaseName
Order By count(DiseaseKey)

need a desc in order by as you want to look in decending order



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vijayrawatsan
Starting Member

18 Posts

Posted - 2010-04-27 : 13:12:30
Thank You Thank YOu Thank YOu
very very very much for such fast replies...

Thanks a LOT.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 13:47:09
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -