dmanisit
asked on
SQL 2005 Query
Ok, I am very new to SQL, I have a query but need to add a field to the results I already have, I am having trouble with this. Here is my current Query:
SELECT * FROM (
SELECT
PM.Patients.Patient_Number , PM.Contacts.Last_Name, PM.Contacts.Middle_Initial , PM.Contacts.First_Name, PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen t_DateTime , PM.Appointments.Scheduling _Location_ ID, PM.Appointments.Status,
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number ORDER BY PM.Appointments.Appointmen t_DateTime ) rn
FROM PM.Appointments INNER JOIN
PM.Patients ON PM.Appointments.Patient_ID = PM.Patients.Patient_ID INNER JOIN
PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin g_Location _ID = 13) AND (PM.Appointments.Status = 'S')) t1 WHERE rn = 1
ORDER BY Last_Name
So I need to add a table called PM.Accounts and the field I need is Account_Type_ID, NOW this is just a numeric value and it is mapped back to the patient through the PM.Patients table, I need to keep the current query I have but add this criteria. One more snag, the actual table that I want to display after matching the Account_Type_ID to the Patient_ID is the table called PM.Account_Types, this is where the Account_Type_ID actually tells me what it is.
SELECT * FROM (
SELECT
PM.Patients.Patient_Number
PM.Appointments.Appointmen
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number
FROM PM.Appointments INNER JOIN
PM.Patients ON PM.Appointments.Patient_ID
PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin
ORDER BY Last_Name
So I need to add a table called PM.Accounts and the field I need is Account_Type_ID, NOW this is just a numeric value and it is mapped back to the patient through the PM.Patients table, I need to keep the current query I have but add this criteria. One more snag, the actual table that I want to display after matching the Account_Type_ID to the Patient_ID is the table called PM.Account_Types, this is where the Account_Type_ID actually tells me what it is.
ASKER
Ok so here it is modified, and the error:
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '('.
USE Ntier_TRAINING
SELECT *
FROM (SELECT PM.Patients.Patient_Number , PM.Contacts.Last_Name,
PM.Contacts.Middle_Initial , PM.Contacts.First_Name,
PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen t_DateTime , PM.Appointments.Scheduling _Location_ ID,
PM.Appointments.Status,
PM.Account_Types.AccountTy pe,Descrip tion
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number ORDER BY PM.Appointments.Appointmen t_DateTime ) rn
FROM PM.Appointments
INNER JOIN PM.Patients ON PM.Appointments.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Accounts ON PM.Accounts.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T ype_ID = PM.Accounts.Account_Type_I D
INNER JOIN PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin g_Location _ID = 13)
AND (PM.Appointments.Status = 'S')) t1
WHERE rn = 1
ORDER BY Last_Name
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '('.
USE Ntier_TRAINING
SELECT *
FROM (SELECT PM.Patients.Patient_Number
PM.Contacts.Middle_Initial
PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen
PM.Appointments.Status,
PM.Account_Types.AccountTy
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number
FROM PM.Appointments
INNER JOIN PM.Patients ON PM.Appointments.Patient_ID
LEFT JOIN PM.Accounts ON PM.Accounts.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T
INNER JOIN PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin
AND (PM.Appointments.Status = 'S')) t1
WHERE rn = 1
ORDER BY Last_Name
You are missing a comma after description
SELECT *
FROM (SELECT PM.Patients.Patient_Number, PM.Contacts.Last_Name,
PM.Contacts.Middle_Initial, PM.Contacts.First_Name,
PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointment_DateTime, PM.Appointments.Scheduling_Location_ID,
PM.Appointments.Status,
PM.Account_Types.AccountType,Description,
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number ORDER BY PM.Appointments.Appointment_DateTime) rn
FROM PM.Appointments
INNER JOIN PM.Patients ON PM.Appointments.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Accounts ON PM.Accounts.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_Type_ID = PM.Accounts.Account_Type_ID
INNER JOIN PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Scheduling_Location_ID = 13)
AND (PM.Appointments.Status = 'S')) t1
WHERE rn = 1
ORDER BY Last_Name
ASKER
Ok so both columns are returning null, I think I see the problem just dont know how to fix it.
here is the whole query
SELECT *
FROM (SELECT PM.Patients.Patient_Number , PM.Contacts.Last_Name,
PM.Contacts.Middle_Initial , PM.Contacts.First_Name,
PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen t_DateTime , PM.Appointments.Scheduling _Location_ ID,
PM.Appointments.Status,
PM.Account_Types.Account_T ype_ID,Des cription,
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number ORDER BY PM.Appointments.Appointmen t_DateTime ) rn
FROM PM.Appointments
INNER JOIN PM.Patients ON PM.Appointments.Patient_ID = PM.Patients.Patient_ID
LEFT JOIN PM.Accounts ON PM.Accounts.Account_type_I D = PM.Patients.Patient_ID
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T ype_ID = PM.Accounts.Account_Type_I D
INNER JOIN PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin g_Location _ID = 13)
AND (PM.Appointments.Status = 'S')) t1
WHERE rn = 1
ORDER BY Last_Name
Here is where I think problem is:
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T ype_ID = PM.Accounts.Account_Type_I D
here is the whole query
SELECT *
FROM (SELECT PM.Patients.Patient_Number
PM.Contacts.Middle_Initial
PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen
PM.Appointments.Status,
PM.Account_Types.Account_T
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number
FROM PM.Appointments
INNER JOIN PM.Patients ON PM.Appointments.Patient_ID
LEFT JOIN PM.Accounts ON PM.Accounts.Account_type_I
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T
INNER JOIN PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin
AND (PM.Appointments.Status = 'S')) t1
WHERE rn = 1
ORDER BY Last_Name
Here is where I think problem is:
LEFT JOIN PM.Account_Types ON PM.Account_Types.Account_T
I think its here
LEFT JOIN PM.Accounts ON PM.Accounts.Account_type_I
Is the Accout_type_ID a patient id field or is there another field to join it to patients
ASKER
It is going to be another field to join
Can you show each table and its fields
ASKER
So this is what I need: This Query works up till what I need to add:
USE Ntier_JSA
SELECT * FROM (
SELECT
PM.Patients.Patient_Number , PM.Contacts.Last_Name, PM.Contacts.Middle_Initial , PM.Contacts.First_Name, PM.Contacts.SSN, PM.Contacts.Date_Of_Birth,
PM.Appointments.Appointmen t_DateTime , PM.Appointments.Scheduling _Location_ ID, PM.Appointments.Status,
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number ORDER BY PM.Appointments.Appointmen t_DateTime ) rn
FROM PM.Appointments INNER JOIN
PM.Patients ON PM.Appointments.Patient_ID = PM.Patients.Patient_ID INNER JOIN
PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin g_Location _ID = 13) AND (PM.Appointments.Status = 'S')) t1 WHERE rn = 1
ORDER BY Last_Name
Now: I need to add just 1 field to the output but it must be matched to that patient. So in the table PM.Accounts is the Account_Type_ID, also in this table there is Account_ID that is relative to the patient we are tryring to match too. So with that said the Account_ID is actually the SAME value that is in another table called PM.Patients and the field is Patient_ID, this is where we will have to match up the correct Account_ID to the Correct Patient_ID. Once thats matched we then need (for the output) to match the Account_Type_ID for that correct patient to output the field called Description
USE Ntier_JSA
SELECT * FROM (
SELECT
PM.Patients.Patient_Number
PM.Appointments.Appointmen
ROW_NUMBER() OVER (PARTITION BY PM.Patients.Patient_Number
FROM PM.Appointments INNER JOIN
PM.Patients ON PM.Appointments.Patient_ID
PM.Contacts ON PM.Patients.Contact_ID = PM.Contacts.Contact_ID
WHERE (PM.Appointments.Schedulin
ORDER BY Last_Name
Now: I need to add just 1 field to the output but it must be matched to that patient. So in the table PM.Accounts is the Account_Type_ID, also in this table there is Account_ID that is relative to the patient we are tryring to match too. So with that said the Account_ID is actually the SAME value that is in another table called PM.Patients and the field is Patient_ID, this is where we will have to match up the correct Account_ID to the Correct Patient_ID. Once thats matched we then need (for the output) to match the Account_Type_ID for that correct patient to output the field called Description
ASKER
This crap just blows my mind
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much. One day I might be able to do it
Open in new window