I have three tables: DriveMaster, Accounts, and CenterDetail.
I'd like to join both DriveMaster and Accounts to CenterDetail on CenterDetail.CenterID.
Would this be the optimal way to accomplish this?
Inner Join rpt_CenterDetail CD on (Acct.CenterID=CD.CenterID) and (DM.CenterID=CD.CenterID)
Or should I do something like alias a second CenterDetail table?
Inner Join rpt_CenterDetail CD1 on Acct.CenterID=CD1.CenterID
Inner Join rpt_CenterDetail CD2 on DM.CenterID=CD2.CenterID
Suggestions?
Your second guess was close, but no cigar. This should work for you:
Select * From rpt_CenterDetail CD
Inner Join rpt_DriveMaster DM on DM.CenterID=CD.CenterID
Inner Join rpt_Accounts A on A.CenterID=CD.CenterID
Hope this helps!
Related
I am doing these joins:
from #lps_at_lines2 as l2
left join jobmatl as jm on l2.item = jm.item
inner join job as j on jm.job = j.job
and jm.suffix = j.suffix
I'm not sure how the join would resolve and the official documentation is like reading hieroglyphics to me.
My guess is that first #lps_at_lines2 gets LEFT JOIN'd to jobmatl and then somehow job gets INNER JOIN'd to jm afterwards. Is that correct?
In a FROM clause, JOINs are parsed left to right -- the reading order in English. So, the LEFT JOIN is processed (logically) before the INNER JOIN.
The INNER JOIN conditions include:
jm.job = j.job and jm.suffix = j.suffix
These refer to the second table of the LEFT JOIN. Because NULL values fail, the INNER JOIN is turning the preceding LEFT JOIN into an INNER JOIN. In other words, you should get the same results using INNER JOIN for both.
Note that you can adjust the prioritization by using parentheses, but your version of the query does not do this.
In general, when mixing inner joins and left joins, I start by including all the inner joins and then LEFT JOINing the additional tables.
I'm trying to write a query that will summarize all of the procedures performed during the calendar year 2013, whether the event happened on a mobile drive or at a fixed site within a particular state.
Every Drive is Unique and will either occur at a Fixed Site (CenterID) or on a Mobile Site, which are associated with Accounts (AccountID). My query looks like this:
select sum(proceduresperformed), sum(productscollected)
from DriveProjectionAndCollectedTotals DPaCT
inner join rpt_DriveMaster DM on DPaCT.DriveID=DM.DriveID
left outer join rpt_Accounts Acct on DM.AccountID=Acct.AccountID
left outer join rpt_CenterDetail CD on DM.CenterID=CD.CenterID
inner join rpt_AddressDetail AD on Acct.AccountID=AD.AccountID
inner join rpt_AddressDetail AD2 on CD.CenterID=AD2.CenterID
where AD.State='FL'
or AD2.State='FL'
and Year(DM.FromDateTime)=2013
But these numbers are really high and incorrect. So what I did was remove one of the joins (either the Account or the CenterDetail) and run the query twice to get numbers that look much more in line with what is expected:
select sum(proceduresperformed), sum(productscollected)
from DriveProjectionAndCollectedTotals DPaCT
inner join rpt_DriveMaster DM on DPaCT.DriveID=DM.DriveID
left outer join rpt_Accounts Acct on DM.AccountID=Acct.AccountID
--left outer join rpt_CenterDetail CD on DM.CenterID=CD.CenterID
inner join rpt_AddressDetail AD on Acct.AccountID=AD.AccountID
--inner join rpt_AddressDetail AD2 on CD.CenterID=AD2.CenterID
where AD.State='FL'
--or AD2.State='FL'
and Year(DM.FromDateTime)=2013
How can I fix the original query to summarize the two columns in a way that does not basically triple the expected value?
The original query returns:
And running the query with Accounts and Centers separately:
I went back and used a UNION statement to give me the correct answer:
select sum(procperf) as 'Procedures Performed'
from (
select sum(proceduresperformed) as procperf
from DriveProjectionAndCollectedTotals DPaCT
inner join rpt_DriveMaster DM on DPaCT.DriveID=DM.DriveID
left outer join rpt_Accounts Acct on DM.AccountID=Acct.AccountID
inner join rpt_AddressDetail AD on Acct.AccountID=AD.AccountID
where AD.State='FL'
and Year(DM.FromDateTime)=2013
UNION
select sum(proceduresperformed) as procperf
from DriveProjectionAndCollectedTotals DPaCT
inner join rpt_DriveMaster DM on DPaCT.DriveID=DM.DriveID
left outer join rpt_CenterDetail CD on DM.CenterID=CD.CenterID
inner join rpt_AddressDetail AD2 on CD.CenterID=AD2.CenterID
where AD2.State='FL'
and Year(DM.FromDateTime)=2013
) as a;
Sorry for Title, don't know how to explain.
Ok so I want to see if any protocol (PTC_ID) is linked to an Audit (AUD_ID), in the picture you can see there is 3 tables and each one has a value of the other.
I though of using inner join all 3 tables with the ON , ON ADA_PTCID = PTC_ID etc. and if a audit is linked with a PTC then display year?
Select AUD_YEAR
From AUD_Table at
Inner Join ADA_TABLE ad
ON at.AUD_ID = ad.ADA_AUD_ID
Inner Join PTC_TABLE pt
ON pt.PTC_ID=ad.ADA_PTCID
try
select
ptc.ptc_name,
aud.aud_year
from
ptc_table ptc
inner join
ada_table ada
on
ada.ada_ptcid=ptc.ptc_id
inner join
aud_table aud
on
aud.aud_id=ada.ada_aud_id
Something like this?
select
aud.year,ptc.name
from ada
inner join aud on ada.aud_id = aud.aud_id
inner join ptc on ada.ptc_id = ptc.ptc_id
Hello in my system i have a search page for a student that the admin will be able to view
the students history i have a problem with showing the last name of his/her adviser which is lname_A. This is the code i currently us so far everything is ok except i cant manage to get the lname_a.
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, d.adviser_id , d.lname_a
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON a.section_id = d.adviser_id
WHERE a.student_id=".$id." AND a.level='Grade 2'";
Would gladly appreciate any help.
Are you sure you are joining both tables or correct columns?
a.section_id = d.adviser_id
If every student has adviser then you should use inner join rather than left outer join.
When you use left outer join there is chance lname_a is empty when the student doesn't has adviser.
I have a Query returning 377 Rows
Select Cm.Customerid,Cm.Customername,Ad.Addressid,Ad.Addressline1,
Stm.Statename,Ctm.Cityname,Dm.Districtname
From Crm.Customers Cm
inner join Crm.Customeraddress Ad on Ad.Customerid=Cm.Customerid
Inner Join Ehis.Statemaster Stm On Stm.Stateid=Ad.Stateid
Inner Join Ehis.Citymaster Ctm On Ctm.Cityid=Ad.Cityid
inner join Ehis.Districtmaster dm on Dm.Districtid=Ad.Districtid
But if i add one more join to this (i.e)
Select
Cm.Customerid,Cm.Customername,Ad.Addressid,Ad.Addressline1,
Stm.Statename,Ctm.Cityname,Dm.Districtname
From Crm.Customers Cm
inner join Crm.Customeraddress Ad on Ad.Customerid=Cm.Customerid
inner join crm.agreements ag on ag.customerid=cm.customerid
Inner Join Ehis.Statemaster Stm On Stm.Stateid=Ad.Stateid
Inner Join Ehis.Citymaster Ctm On Ctm.Cityid=Ad.Cityid
inner join Ehis.Districtmaster dm on Dm.Districtid=Ad.Districtid
My query is not returning any rows. Is there any Problem in using inner join for statemasters,city n district masters. Pls clarify the same.
The problem doesn't appear to be with statemaster, citymaster or districtmaster, as these are in your original query that returns data.
The additional line (below) looks to be the culprit.
inner join crm.agreements ag on ag.customerid=cm.customerid
Presumably there are no records on the agreements table with a matching customerid on the customers table.
To prove that is the case, you could change that line to
left join crm.agreements ag on ag.customerid=cm.customerid
If you get your 377 records back, then you'll need to check the data in agreements.