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
 General SQL Server Forums
 New to SQL Server Programming
 SELECT * FROM 2 different tables?

Author  Topic 

frozenx
Starting Member

2 Posts

Posted - 2008-08-17 : 08:58:19
Tables I need to use for this query
Patient (patientNo, patName, patAddr, DOB)
Prescribed (patientNo, drugNo, unitsPerDay, startDate, finishDate)

Other tables
Ward (wardNo, wardName, wardType, noOfBeds)
Contains (patientNo, wardNo, admissionDate)
Drug (drugNo, drugName, costPerUnit)

I'm looking to list the details of all patients and thier prescribed drugNo and number of units of their medication. I have only limited knowledge of SQL and how to get around this query is confusing me!

Any help would be appreciated. Many thanks! :-)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-17 : 09:02:58
[code]SELECT p.patientNo,
p.patName,
pr.drugNo,
pr.unitsPerDay,
pr.unitsPerDay * DATEDIFF(dd,pr.startDate, pr.finishDate) AS TotalUnits
FROM Patient p
INNER JOIN Prescribed pr
ON pr.patientNo=p.patientNo
[/code]
Go to Top of Page

frozenx
Starting Member

2 Posts

Posted - 2008-08-17 : 09:04:28
You absoloute legend! I'm going to go try it now on my test database, thanks very much. :-)
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 2008-08-18 : 04:59:03
Visakh16

Why did you use inner join. Wouldn't just join alne word with that?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 05:01:45
quote:
Originally posted by Deon Smit

Visakh16

Why did you use inner join. Wouldn't just join alne word with that?


Sorry didnt get you. Were you asking if JOIN is same as INNER JOIN? if yes, yup they are one and the same. I just specified the type of join explicitly. thats all.
Go to Top of Page
   

- Advertisement -