I need help in creating a stored procedure that pulls out multiple data from different tables.
My current stored procedure is as follows:
'#partnername nvarchar(120)
as
select ProjectDetails.Project, ProjectDetails.Id
from ProjectDetails
join ProjectPartners on ProjectPartners.ProjectDetailsId = ProjectDetails.Id
join Partners on Partners.Id = ProjectPartners.PartnersId
where Partners.PartnerName= #partnerName'
This Stored procedure allows a user to insert a partner name, this then displays the projects they are linked within.
But now I'm wishing to display more data within the stored procedure from other tables such as the following:
Table (ProjectFinance) columns ID, ProjectValue, FundingAgency and AgencyValue
Table (Partnership) Columns ID, PartnershipLevel, PartnershipType.
The Tables are linked using foreign keys within the project finance table & Partnership table these Foreign Keys are known as ProjectDetailsID
Any help will be greatly appreciated!
You need to add the tables to your joins and add their columns to your select list:
select ProjectDetails.Project, ProjectDetails.Id, pf.*, p.*
from ProjectDetails
join ProjectPartners on ProjectPartners.ProjectDetailsId = ProjectDetails.Id
join Partners on Partners.Id = ProjectPartners.PartnersId
join ProjectFinanct pf on pf.ProjectDetailsId = ProjectDetails.ID
join Partnership p on p.ProjectDetails.ID = ProjectDetails.ID
where Partners.PartnerName= #partnerName'
Related
Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key
I'm having trouble doing the following query. The idea is that I have two tables Stores and Users. In Stores I have the columns store_owners and store_last_modified, both values are integer that are related to the id of dbo.Users. How I can display the name that is stored in users related to the two columns. Like that:
select stores.name , users.name as name_store_owner , users.name as name_store_last_modified
from stores
LEFT JOIN users ON stores.store_owners=users.id (related to name_store_owner)
LEFT JOIN users ON stores.store_last_modified=users.id (related to name_store_last_modified)
How do I do that?
Thank you in advance.
You need to give the tables aliases, so you can refer to the same table twice in the from clause. In addition, you need to refer to the right table (users not stores):
select s.name, uo.name as name_store_owner, um.name as name_store_last_modified
from stores s left join
users uo
on s.store_owners = uo.id left join
users um
on s.store_last_modified = um.id
It appears that the condition can be checked without referring the table twice.
select * from
stores s
left join
users u on u.id = s.store_owners
and u.id = s.store_last_modified
From your question it appears that user id (id) should match with both the columns in the stores table for a single row.
I have two tables: one called EMP_Names which simply stores ID and Employee_Name and another table called EMP_Main which stores the main data and which refers to EMP_Names via IDs. Amongst other fields EMP_Main has fields called Technician_Name_ID and Leader_Name_ID which is related to EMP_Names. My problem is this: how can i run a query where both Technician_Name_ID and Leader_Name_ID resolve to Names? In other words both ID fields refer to the same EMP_Names.ID but I can only establish one relationship between the two tables.
Don't know if I'm clear because it's difficult to explain ...
You can use join but you need multiple joins.
select em.*, ent.name as technician, enl.name as leader
from (emp_main as em left join
emp_names as ent
on em.technician_name_id = ent.id
) left join
emp_names as enl
on em.leader_name_id = enl.id;
These are left joins in case the fields are not populated for all rows.
I'm using an Access Database that is designed relationally. I have a list of patient IDs in an Excel spreadsheet that I imported and made into a table in Access named importedPatients. There is a related table named Counties that is a one to many relationship for the main Patients table. How can I use the patientIDs present in the importedPatients table to quickly query out information in the related Counties table? Is there an idiomatic way to tackle this?
This diagram can give you an idea:
and here is SQL generated by the Designer:
SELECT importedPatients.ImportedPatientID, Patients.PatientName, Counties.County
FROM (Counties INNER JOIN Patients ON Counties.CountyID = Patients.CountyID)
INNER JOIN importedPatients ON Patients.PatientID = importedPatients.ImportedPatientID
This will give you county information for each record in importedPatients:
SELECT counties.*
FROM counties INNER JOIN (importedpatients INNER JOIN patients ON
importedpatients.patientid = patients.patientID) ON
counties.countyid = patients.countyid
I am new to SQL. I am trying to select some of the fields from 2 of 3 joined tables and all of the fields from a 3rd table. Other than specifying each individual field from the Pr table, is the a simpler way to SELECT individual fields from two of the tables and SELECT * from the 3rd table. My current query is below. This is an exercise from a book. If the 3rd table contained more fields this method would become very cumbersome.
SELECT Lo.City ,
Em.FirstName ,
Em.LastName ,
Pr.EmpID ,
Pr.YearlySalary ,
Pr.MonthlySalary ,
Pr.HourlyRate
FROM Location AS Lo
INNER JOIN Employee AS Em ON Lo.LocationID = Em.LocationID
INNER JOIN PayRates AS Pr ON Em.EmpID = Pr.EmpID
Yes, you can use Pr.* to select all columns from the table aliased Pr.
This is widely discouraged in production code however as if the table definition changes your query can suddenly start bringing back loads of irrelevant data that is not needed.