When i run Following Query everything works well but the subQuery inside LEFT JOIN Fails and
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN (
SELECT rc1.* FROM rentalcode as rc1 WHERE rc1.assetCategory=
(SELECT AssetCategory FROM assetmaster WHERE
AssetID=invoice_lineitem.asset_typeID)
AND invoice_lineitem.invoice_date between rc1.start_date
AND rc1.validity_date
) rc ON rc.email=inv.usernameID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10'
I get Error as
1054 - Unknown column 'invoice_lineitem.asset_typeID' in 'where
clause'
When i try the same query by just removing the all invoice_lineitem from SubQuery and placing it in WHERE(beside contractID) the query works but it acts like INNER JOIN Query i.e if Query Fails for any of the Where Clause i get No Data
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN (
SELECT rc1.* FROM rentalcode as rc1
) rc ON rc.email=inv.usernameID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10' AND rc.assetCategory=(SELECT AssetCategory FROM assetmaster WHERE AssetID=inv.asset_typeID)
AND inv.invoice_date between rc.start_date and rc.validity_date
A subquery can't refer to tables in the outer query (and if it could, you would have to use the inv. alias).
Try this. I changed the subquery into another LEFT JOIN.
SELECT inv.*,rc.*,po.*,cfd.*
FROM invoice_lineitem as inv
LEFT JOIN rentalcode AS rc1
ON rc.email = inv.usernameID
AND inv.nvoice_date between rc1.start_date AND rc1.validity_date
LEFT JOIN assetmaster AS am
ON rc1.assetCategory = am.AssetCategory
AND inv.asset_typeID = am.AssetID
LEFT OUTER JOIN purchase_order as po ON (po.PO_ID=inv.poNumber)
LEFT OUTER JOIN client_facility_data as cfd ON (cfd.email=inv.usernameID)
WHERE inv.contractID='2/1AS/10'
Related
I have the following tables and relationships
Here are the sample data -
I tried the following query-
SELECT project.*, SUM(schedule.amount) as sch_total, SUM(expense.amount) as expense_total, SUM(bill.amount) as bill_total from project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN expense on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id
This is the query result -
But "bill_total" should be 200, not 400.
What did I miss in the query?
Your expense table have multiple rows for same Id. That table must be aggregated first and then used in join -
SELECT project.*,
SUM(schedule.amount) as sch_total,
SUM(E.amount) as expense_total,
SUM(bill.amount) as bill_total
FROM project
LEFT OUTER JOIN schedule on project.project_id = schedule.project_id
LEFT OUTER JOIN site on project.project_id = site.project_id
LEFT OUTER JOIN (SELECT site_id, SUM(expense.amount)amount
FROM expense
GROUP BY site_id) E on site.site_id = expense.site_id
LEFT OUTER JOIN bill on site.site_id = bill.site_id
GROUP BY project.project_id
After following this thread. I am getting an error as "Syntax Error in JOIN Operation"
Here Is my query
SELECT
StudentMaster.Student_id,
ExamDetails.Subject_id,
SubjectMaster.Subject_id
FROM
(StudentMaster
LEFT OUTER JOIN ExamDetails
ON StudentMaster.Student_id = ExamDetails.Student_id)
LEFT OUTER JOIN ExamDetails
ON SubjectMaster.subject_id = ExamDetails.Subject_id
Any help regarding this would be appreciable
Have you tried this one.
select s.Student_id,e.Subject_id,sub.Subject_id
from YourDatabaseName.SubjectMaster as sub
left outer join YourDatabaseName.ExamDetails as e
on sub.Subject_id=e.Subject_id
left outer join YourDatabaseName.StudentMaster as s
on s.Student_id=e.Student_id
group by s.Student_id
order by s.Student_id;
I have this code..
SELECT u.*, COALESCE(c2.name,u.state) AS real_state, c.name AS real_country, bc.alpha3 AS country_iso FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND real_state=bs.name
WHERE u.blesta_id=0;
The problem likes in this area:
COALESCE(c2.name,u.state) AS real_state
and
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND real_state=bs.name
I am getting this error:
Error Code: 1054
Unknown column 'real_state' in 'on clause'
How can I use the result of COALESCE(c2.name,u.state) in a JOIN?
Thanks!
Does gimko_blesta.states even need to be in the source tables? None of its columns are in the select list/criteria, and it's joined in with a LEFT JOIN, so doesn't seem like it's necessary.
In any case, you would need to make it a derived table first, and join gimko_blesta.states to the derived table, or just use the expression in the JOIN, like COALESCE(c2.name,u.state) = bs.name, below:
SELECT u.*, COALESCE(c2.name,u.state) AS real_state, c.name AS real_country, bc.alpha3 AS country_iso
FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
LEFT JOIN gimko_blesta.states AS bs ON bc.alpha2=bs.country_alpha2 AND COALESCE(c2.name,u.state) = bs.name
WHERE u.blesta_id=0;
You can't use virtual aliases at the same level of your query you may have to repeat the whole expression or use a sub select to access in your parent query
SELECT * FROM (
SELECT u.*,
COALESCE(c2.name,u.state) AS real_state,
c.name AS real_country,
bc.alpha3 AS country_iso ,
bc.alpha2
FROM gimko_v2.users AS u
INNER JOIN gimko_v2.countries AS c ON u.country=c.id
LEFT JOIN gimko_v2.countries AS c2 ON u.state=c2.id
LEFT JOIN gimko_blesta.countries AS bc ON c.country_iso_code=bc.alpha2
) t
LEFT JOIN gimko_blesta.states AS bs ON t.alpha2=bs.country_alpha2 AND t.real_state=bs.name
WHERE t.blesta_id=0;
Also in sub-select you have to select all the needed columns that will be used in outer join for parent query like in your case i have selected bc.alpha2 in inner query because to join with states this column is needed
I currently have the following Query
SELECT * FROM tbl_Cars c
INNER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
INNER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
However I have now realised that there are some Ids which are in the Cars Table which are then not in the Car Parts table so these are being omitted from my results while I want them included. I tried changing my INNER JOIN to a LEFT OUTER JOIN but it made no difference in the result set I am returned?
Use LEFT OUTER JOIN in both of the joins.
SELECT * FROM tbl_Cars c
LEFT OUTER JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT OUTER JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
Otherwise the second INNER JOIN will invalidate your first LEFT OUTER JOIN for those records that does not have ID (does not join) in the tbl_Car_Parts table.
After a LEFT OUTER JOIN you may be only use again INNER JOIN if the table you are joining is not related with the previous ones that are joined using the LEFT OUTER JOIN.
LEFT JOIN should have definitely solved your problem, Not sure why it didnt work.
Just to be safe I used Left join on both the tables...
SELECT * FROM tbl_Cars c
LEFT JOIN tbl_Car_Parts cp ON c.Id = cp.Id
LEFT JOIN tbl_Car_Factory cf ON cp.Id = cf.Id
Following query does not work in access.
SELECT Fields.FieldId, PrecisionSettings.DecimalPlaces
from Fields left outer join FieldGroup on Fields.FieldGroupId = FieldGroup.FieldGroupId
left outer join Category on FieldGroup.CategoryId = Category.CategoryId
left outer join PrecisionSettings on
Category.InputAttributesID=PrecisionSettings.AttributesID
It gives error as missing operator in query expression.
In Access you can only join two results at a time. To join more tables you need more parentheses:
SELECT
Fields.FieldId,
PrecisionSettings.DecimalPlaces
FROM
(
(
Fields
LEFT OUTER JOIN FieldGroup ON Fields.FieldGroupId = FieldGroup.FieldGroupId
)
LEFT OUTER JOIN Category ON FieldGroup.CategoryId = Category.CategoryId
)
LEFT OUTER JOIN PrecisionSettings ON Category.InputAttributesID = PrecisionSettings.AttributesID