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;
Related
I have a database with multiple tables, I wish to show all contents from every table with one query (that I will use later for a research in the database).
view of the database
I've tried with two tables
[SELECT
reception.num_courrier, reception.date_recep, reception.expediteur, reception.objet,
lect_prefet.date_lect_prefet, lect_prefet.instructions, lect_prefet.caractere
FROM
reception
FULL JOIN
lect_prefet ON reception.num_courrier = lect_prefet.num_courrier][2]
but it returns an error.
I'm using 10.4.14-MariaDB version.
full join query error
One method uses multiple left joins, starting with all available ids:
SELECT *
FROM (SELECT r.num_courrier
FROM reception r
UNION -- on purpose to remove duplicate
SELECT l.num_courrier
FROM lect_prefet l
) c LEFT JOIN
reception r
USING (num_courrier) LEFT JOIN
lect_prefet l
USING (num_courrier);
This approach easily generalizes to more tables using the same key for the JOIN.
Please try the following:
SELECT * FROM reception r
LEFT OUTER JOIN lect_prefet lp ON lp.num_courrier = r.num_courrier
LEFT OUTER JOIN lect_sg ls ON ls.num_courrier = r.num_courrier
LEFT OUTER JOIN ad ad ON ad.num_courrier = r.num_courrier
LEFT OUTER JOIN agt agt ON agt.num_courrier = r.num_courrier
If the above doesn't work, it would be great if you can provide an SQLFiddle example so that others can try on a live schema.
Thank you all, you really helped me, this is what I expected, folowing the answer of #Gordon Linoff[ adding some rows] Thank you
SELECT * FROM (
SELECT r.num_courrier FROM reception r UNION
SELECT l.num_courrier FROM lect_prefet l UNION
SELECT s.num_courrier FROM lect_sg s UNION
SELECT g.num_courrier FROM agt g UNION
SELECT d.num_courrier FROM ad d ) c
LEFT JOIN reception r USING (num_courrier)
LEFT JOIN lect_prefet l USING (num_courrier)
LEFT JOIN lect_sg s USING (num_courrier)
LEFT JOIN agt g USING (num_courrier)
LEFT JOIN ad d USING (num_courrier);
Expected
Answer
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!
SELECT
s.session_name semester
FROM
lsx.elsp_session s,
lsx.elsp_course_offering co,
its.ACCOUNT ac
LEFT OUTER JOIN lsx.elsp_class_attendance ca on ca.course_id = CO.ID and ca.student_number = ac.id
WHERE co.session_id = s.session_id
I am getting an ORA-00904 error and the problem seems to be with "CO.ID" being an invalid identifier. However, if I put the "lsx.elsp_course_offering co" table to be last in the FROM list, then "ac.id" becomes the problem identifier.
Is it not possible to use another table in a JOIN clause? I seem to recall creating successful queries like this using MySQL.
I am using Oracle 9.2.0.8.0.
Following the advice I received, I was able to get the join working as expected when I reworked the query to this:
SELECT s.session_name semester FROM lsx.elsp_session s
INNER JOIN lsx.elsp_course_offering co
ON co.session_id = s.session_id
LEFT OUTER JOIN lsx.elsp_class_attendance ca
ON ca.course_id = co.id
LEFT OUTER JOIN its.account ac
ON ca.student_number = ac.id
Many thanks for your help.
You may have misunderstood the comment above. Generally people do not mix the multiple FROM tables with JOIN syntax.
I think you should write it this way instead. It should be easier to follow this way.
SELECT s.session_name semester
FROM lsx.elsp_session s
INNER JOIN lsx.elsp_course_offering co
ON co.session_id = s.session_id
INNER JOIN its.account ac
ON ca.student_number = ac.id
LEFT OUTER JOIN lsx.elsp_class_attendance ca
ON ca.course_id = co.id
and then see if you get syntax errors.
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.