I have a table called Doctor.
Doctor table consist of 4 fields. DoctorID,DoctorName, DoctorAddress, DoctorSpeciality
I have another table called PatientData. and it has 4 fields in it. PatientId, PatientName, PatientTelephone, DoctorID.
I need to write a SQL that would display the following fields;
PatientID, PatientName, DoctorName, DoctorSpeciality
1.) I think, i will have to use an INNER JOIN here, but i am not sure how to write it for this scenario. An outer join would also work i guess, but i am new to Joins. Can someone please help me here ?
2.) Can i create a VIEW for the SQL statement that i am creating above ?
Something like this should would using a regular INNER JOIN -- this will return all records from the Doctor table with a matching record in the PatientData table:
SELECT PD.PatientId, PD.PatientName, D.DoctorName, D.DoctorSpecialty
FROM Doctor D
INNER JOIN PatientData PD ON D.DoctorId = PD.DoctorId
If you want to return all data from one of the other tables, look into using a OUTER JOIN (I prefer LEFT JOINs).
Here's a nice article on visual representation of joins: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
And yes, you can create a view if you'd like -- depends on your needs. Something like this should be close:
CREATE VIEW DoctorPatients AS
SELECT PD.PatientId, PD.PatientName, D.DoctorName, D.DoctorSpecialty
FROM Doctor D
INNER JOIN PatientData PD ON D.DoctorId = PD.DoctorId
SQL Server Views: http://msdn.microsoft.com/en-us/library/ms187956.aspx
It is a simple join
SELECT p.PatientID, p.PatientName, d.DoctorName, d.DoctorSpeciality
FROM PatientData p JOIN Doctor d on d.DoctorID = p.PatientID
Of course you could create a view from this
CREATE VIEW [dbo].[PatientAndDoctor]
AS
SELECT p.PatientID, p.PatientName, d.DoctorName, d.DoctorSpeciality
FROM PatientData p JOIN Doctor d on d.DoctorID = p.PatientID
Related
I am working on a query that has fields called ios_app_id, android_app_id and app_id.
The app_id from downloads table can be either ios_app_id or android_app_id in the products table.
Is it correct that because of that I cannot just run a simple join of downloads and products table on on p.ios_app_id = d.app_id and then join again on on p.android_app_id = d.app_id? Would that cause an incorrect number of records?
select p.product_id, d.date, d.downloads,
from products p
inner join download d
on p.ios_app_id = d.app_id
UNION
select p.product_id, d.date, d.downloads
from products p
inner join download d
on p.android_app_id = d.app_id
I would try:
select p.product_id, d.date, d.downloads,
from products p
inner join downloads d
on p.ios_app_id = d.app_id
inner join downloads d
on p.android_app_id = d.app_id
Basically I am trying to understand why the union here is needed instead of just joining the two fields twice? Thank you
Just join twice:
select p.product_id,
coalesce(di.date, da.date),
coalesce(di.downloads, da.downloads)
from products p left join
downloads di
on p.ios_app_id = di.app_id left join
downloads da
on p.android_app_id = da.app_id;
This should be more efficient than your method with union. Basically, it attempts joining using the two ids. The coalesce() combines the results into a single column.
Remember that the purpose of an INNER JOIN is to get the values that exists on BOTH sets of data (lets called them table A and table B), using a specific column to join them. In your example, if you try to do the INNER JOIN twice, what would happen is that the first time you execute the INNER JOIN, the complete PRODUCTS table is your table A, and you obtain all the products that have downloaded the ios_app, but now (and this is the key part) this result becomes your new dataset, so it becomes your new table A for the next inner join. And thats the issue, cause what you would want is to join the whole table again, not just the result of the first join, but thats not how it works. This is why you need to use the UNION, cause you need to obtain your results independently and then add them.
An alternative would be to use LEFT JOIN, but you could get null values and duplicates -and its not too "clean"-. So, for your particular case, I think using UNION is much clearer and easier to understand.
If you do left join in first query it will work.
create table all_products as (select p.product_id, d.date, d.downloads,
from products p
left join downloads d
on p.ios_app_id = d.app_id)
select a.product_id, d.date, d.downloads from all_products a left join downloads d
on a.android_app_id = d.app_id inner join
I have to create a view that joins together all of the columns in the CUSTOMERS, ORDERS, ORDERDETAILS, EMPLOYEES, PAYMENTS and PRODUCTS tables.
the schema for the table is below
I tried the following query, though I am at a loss how to solve the above question :
create view orders_view AS
select *
from sys.customers c
left JOIN EMPLOYEES e
on c.SALESREPEMPLOYEENUMBER = e.EMPLOYEENUMBER
left join sys.orders o
on c.CUSTOMER NUMBER = o.CUSTOMERNUMBER
left join sys.orderdetails od
on o.ORDERNUMBER = od.ORDERNUMBER
left join sys.products p
on od.PRODUCTCODE = p.PRODUCTCODE
left join sys.PAYMENTS py
on c.CUSTOMERNUMBER = py.customernumber
I am a newbie with SQL and databases, so any help is appreciated.
Here are some observations on things going wrong:
You have curly braces that are not necessary (perhaps this is a typo in the question).
You are selecting *, but have duplicate column names (such as productcode), which prevents the view from being created. Best practice: list all the columns explicitly in the view.
You have c.CUSTOMER NUMBER = o.CUSTOMERNUMBER. The space is probably a typo. If not, change the name so the space is not part of the name. Best practice: Use identifiers that do not have to be escaped.
I am not aware of a sys.customers table. The sys schema should only be used for internal Oracle objects. (Here is one source.)
thank you for all the inputs. they helped me a lot in figuring out the answer.
Following is the query for the question asked above.it gave me a view with columns from all tables.
create or replace view overall AS
select c.*,
e.LASTNAME,
e.FIRSTNAME,
e.EXTENSION,
e.EMAIL,
e.OFFICECODE,
e.REPORTSTO,
e.JOBTITLE,
o.ORDERNUMBER,
o.ORDERDATE,
o.REQUIREDDATE,
o.SHIPPEDDATE,
o.STATUS,
o.COMMENTS,
od.PRODUCTCODE,
od.QUANTITYORDERED,
od.PRICEEACH,
od.ORDERLINENUMBER,
p.PRODUCTNAME,
p.PRODUCTLINE,
p.PRODUCTSCALE,
p.PRODUCTVENDOR,
p.PRODUCTDESCRIPTION,
p.QUANTITYINSTOCK,
p.BUYPRICE,
p.MSRP,
py.CHECKNUMBER,
py.PAYMENTDATE,
py.AMOUNT
from sys.customers c
left JOIN EMPLOYEES e
on c.SALESREPEMPLOYEENUMBER = e.EMPLOYEENUMBER
left join sys.orders o
on c.CUSTOMERNUMBER = o.CUSTOMERNUMBER
left join sys.orderdetails od
on o.ORDERNUMBER = od.ORDERNUMBER
left join sys.products p
on od.PRODUCTCODE = p.PRODUCTCODE
left join sys.PAYMENTS py
on c.CUSTOMERNUMBER = py.customernumber
;
Your query looks alright, but I don't think it will let you create a view if more than one column has the same name.
Since there are duplicates, e.g. CITY, I think the only way around it is to name all the columns and give unique names for duplicate columns.
I have three tables in sql-server like table A table B table C.
How can I join 3 tables as expressed in the image below?
More information needed to give you a correct piece of code, but from the image you need LEFT JOINs.
(ID's have been presumed)
SELECT *
FROM Customers c
LEFT JOIN Items i ON c.iid = i.id
LEFT JOIN Sales s ON c.sid = s.id
It's never too late :)
Most probably you'll need this:
select ...
from customers
full outer join (items inner join sales on (xxx)) on (xxx)
I couldnt think of a very good title so let me explain.
I have a list of days that students were absent (dbo.Attendance).
I also have a list of days that school was open (dbo.DateCollection).
My question is..
Is there some why for me to join/combined the two table into a list that shows each day for each student with their attendance? something like this..
Any help or direction would be well appreciated!!
Yes. You can do this by joining the tables. In this case, it is a bit tricky, because you have to start with a row for all students on all days, and then see if the student was present. Let me assume that you have a Students table:
select s.personid, c.calendarid, c.date, coalesce(a.absentMinutes, 0) as absentMinutes
from Calendar c cross join
Students s left outer join
Attendance a
on a.dateOccurred = c.date and
a.personid = s.personid;
If you don't have students table, then you have a problem. You can only report on students that are in the Attendance table. You could do this as:
select s.personid, c.calendarid, c.date, coalesce(a.absentMinutes, 0) as absentMinutes
from Calendar c cross join
(select distinct personid from Attendance) s left outer join
Attendance a
on a.dateOccurred = c.date and
a.personid = s.personid;
Thanks for all the input.
I ended up using a bridge table for students as well..
This is what I have got working for me.
SELECT s.personid,
dc.calendarid,
dc.[date],
coalesce(absentMinutes,0) AS absentMinutes
FROM [dbo].[StudentCalendar] s INNER JOIN
[dbo].[DateCollection] dc ON s.calendarId=dc.calendarId LEFT JOIN
[dbo].[Attendance] a ON a.dateOccurred=dc.[date] AND a.personId=s.personId
I have three tables:
COLLECTION
PERSON
PERSON_COLLECTION
where PERSON_COLLECTION is a mapping table id|person_id|collection_id
I now want to select all entries in collection and order them by person.name.
Do I have to join the separate tables with the mapping table first and then do a join again on the results?
SELECT
c.*,
p.Name
FROM
Collection c
JOIN Person_Collection pc ON pc.collection_id = c.id
JOIN Person p ON p.id = pc.person_id
ORDER BY p.Name
Not sure without the table schema but, my take is:
SELECT
c.*,
p.*
FROM
Person_Collection pc
LEFT JOIN Collection c
ON pc.collection_id = c.id
LEFT JOIN Person p
ON pc.person_id = p.id
ORDER BY p.name
The order you join won't break it but depending on which sql product you're using may effect performance.
You need to decide if you want ALL records from both/either table or only records which have a matching mapping entry, this will change the type of join you need to use.