Join expression not supported SQL - sql

SELECT
Trs.itemID, Trs.imtName, Trs.sumQty, Sum(whiQty)
FROM
((SELECT
trsitemID AS itemID, trsimtName AS imtName,
Sum(trsQty) As sumQty
FROM
tblTransactionSub AS T
WHERE
trstraID = 1231
AND trsActive = True
GROUP BY
trsitemID, trsimtName) AS Trs
INNER JOIN
tblWarehouseItem AS WHI ON Trs.itemID = WHI.whiitemID)
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
WHERE
whiActive = True
AND whiCansel = False
AND whiwrhID = 19
GROUP BY
Trs.itemID,Trs.imtName, Trs.sumQty
HAVING
SUM(whiQty) < Trs.sumQty
If you please help me me out since I am new to SQL commands I can not easily find my mistake.
Thanks in advance
The error that occurred when I added the Right Join is:
Join expression not supported

In MS Access, you have to use parenthesises with multiple joins:
select ...
from
((table1
... join table2 on ...)
... join table3 on ...)
... join tableN
/edit/
As OP question changes its syntax often, then my answer seems out of place :) Initially there were no parens there.
About RIGHT JOIN: You need to use table name (or entire subselect) after JOIN keyword, not skip it or use some other alias. Your query part
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
currently uses alias WHI, which is wrong in two ways: 1) it is not table name 2) it is already used. You need something like this:
RIGHT JOIN
tblWarehouseItem AS WHI2 ON Trs.trswhiID = WHI2.whiID
It could be possible that MS Access restricts your kind of JOINs usage (like INNER join should not come after LEFT join); I have currently no possibility to check precise rules.

Your problem is that you have no table name after the RIGHT JOIN:
RIGHT JOIN
ON Trs.trswhiID = WHI.whiID
Should be:
RIGHT JOIN YOURTABLENAMEHERE as alias
ON Trs.trswhiID = WHI.whiID
However, you have already defined Trs and Whi, so I have no idea what table you want there, or why. Perhaps you just want to change the INNER JOIN to a LEFT JOIN or RIGHT JOIN.

Related

What type of Join Syntax is this?

So I've never seen a join syntax like this before, and I am curious at what it is called and what the use case is for it?
It seems to preforms similar to when you join to a subquery, without actually creating a subquery.
from member ddm
inner join trace_execution ssn on ddm.id = ssn.id
**left join dw_member_subscription dms
inner join dw_d_subscription dds on dms.id = dds.id
on ddm.id = dms.id**
JOINs can be nested like this. It is interpreted as:
from member ddm inner join
trace_execution ssn
on ddm.id = ssn.id left join
(dw_member_subscription dms inner join
dw_d_subscription dds
on dms.id = dds.id
)
on ddm.id = dms.id
I wouldn't write SQL like this in general. However, I wish the standard required parentheses to make it clear what is going on when the JOINs are not interleaved with their corresponding ON clauses.

Right and Left Join Together in SQL

I am trying to use a RIGHT and LEFT Join together. I have it working when only left joining one table. But I am trying to now include another table in my left join. It gives me an error saying I am missing an operator. Where am I missing a parenthesis?
FROM qSplit RIGHT JOIN (t_i360_agent AS i LEFT JOIN cmsAgent_Split AS c
ON ((i.LocalDay = c.LocalDay) AND (i.ACDID = c.LOGID))
LEFT JOIN qry_AllNewtables as qry ON (qry.custConvDate = c.LocalDay)
AND (qry.CustAgentLoginName = i.Loginname) ) ON qSplit.SPLIT = c.SPLIT
Don't use LEFT JOIN and RIGHT JOIN together. I imagine that somewhere there could be a query where it makes sense. In practice, I don't think I have ever used them both in the same query, possibly because I write queries using LEFT JOIN.
If you want everything in the agent table, then make it first! And use left join;
FROM t_i360_agent i LEFT JOIN
cmsAgent_Split c
ON i.LocalDay = c.LocalDay AND i.ACDID = c.LOGID LEFT JOIN
qry_AllNewtables qry
ON qry.custConvDate = c.LocalDay AND
qry.CustAgentLoginName = i.Loginname LEFT JOIN
qSplit
ON qSplit.SPLIT = c.SPLIT
It is much easier to follow the intention of the query this way. You are starting with the data that you think is so important that you want to keep all of it, even when JOINs have no matching rows.
Beside the suggestion by Gordon the problem was you miss a join condition for the RIGHT JOIN once I reformat the query was easy to see what was missing.
FROM qSplit
RIGHT JOIN t_i360_agent AS i
-- Need join condition
LEFT JOIN cmsAgent_Split AS c
ON (i.LocalDay = c.LocalDay)
AND (i.ACDID = c.LOGID))
LEFT JOIN qry_AllNewtables as qry
ON (qry.custConvDate = c.LocalDay)
AND (qry.CustAgentLoginName = i.Loginname)

SQL syntax: outer table_name

I have a project with this query that I don't get something in it.
Select
c.prop_table1,
(SELECT max(t.taux_table_x) FROM DB3:table_x t
WHERE month(t.date_prop_x) = month(c.prop_of_x)
AND year(t.date_prop_x) = year(c.prop_of_x)
)*1 table_x,
''||date(c.prop_of_x) Alias
FROM DB1:table_name1 d, DB1:table_name2 c, outer DB2:table_name a
WHERE
c.value_i = d.value_i AND
c.property =d.property AND
(c.num_table_name2 =a.no_dos_adh OR c.ref_cred=a.no_dos_adh) AND
c.abcd not in ('P','S')
I don't know what outer is expressing exactly. Is it a left/right or full join? Or is it something else?
thanks for the help everyone
It looks a lot like your query is written using the (IBM) Informix SQL dialect. If that is the case then the outer should be a left outer join as it has a table on its left side (I believe a right join would be from outer tab1, tab2)
The syntax predates the SQL-92 standard but should still be valid (?) with Informix.
Rewritten as standard SQL it would probably look like this:
SELECT
c.prop_table1,
(
SELECT max(t.taux_table_x) FROM DB3:table_x t
WHERE month(t.date_prop_x) = month(c.prop_of_x)
AND year(t.date_prop_x) = year(c.prop_of_x)
)*1 table_x,
'' || date(c.prop_of_x) Alias
FROM
DB1:table_name1 d
INNER JOIN
DB1:table_name2 c ON c.value_i = d.value_i AND c.property = d.property
LEFT JOIN
DB2:table_name a ON (c.num_table_name2 = a.no_dos_adh OR c.ref_cred = a.no_dos_adh)
WHERE
c.abcd not in ('P','S')
I don't know what outer is expressing exactly is it a left/ right or full join?
Not withstanding syntax problems with your sql code, outer is the set union of left and right. In your example, this is the equivalent
(table1,table2) union (table3)
But your SQL is sorely incomplete to give any more specific answers.

Hibernate criteria query for join with condition

I have the following SQL query:
SELECT
tos.ID
FROM
TESTCYCLE_OPCO_SETTING tos
JOIN OPCO o
ON o.ID = tos.OPCO_ID
JOIN TESTCYCLE tc
ON tc.ID = tos.TESTCYCLE_ID
LEFT OUTER JOIN BUILD b
ON b.ID = tc.BUILD_ID
LEFT OUTER JOIN BUILD_OPCO_SETTING bos
ON bos.BUILD_ID = b.ID
AND bos.OPCO_ID = o.ID
What is needed is that the OPCO_ID form the TESTCYCLE_OPCO_SETTING is used to fetch the corresponding BUILD_OPCO_SETTING. I have done this with the "AND" condition in the last JOIN.
I now want to create a Hibernate Critera query out of this and created the following aliases for the joins:
criteria.createAlias("opco", "opco");
criteria.createAlias("testcycle", "testcycle");
criteria.createAlias("testcycle.build", "build", CriteriaSpecification.LEFT_JOIN);
criteria.createAlias("build.buildOpcoSettings", "buildOpcoSetting", CriteriaSpecification.LEFT_JOIN, Restrictions.eqProperty("buildOpcoSetting.opco.id", "opco.id"));
However the 4th alias throws an SQL error and I think I'm using this incorrectly for what I want to achieve. If I replace the property "opco.id" with a specific numeric value it works but this doesn't help me of course.
Any ideas?
Found the solution myself. Using the "opco" alias within the "buildOpcoSetting" alias lead to a wrong order of joins in the resulting SQL and an extra "opco" join. Replaced it with the opco reference from the "testcycleOpcoSetting" which works fine.
criteria.createAlias("build.buildOpcoSettings", "buildOpcoSetting", CriteriaSpecification.LEFT_JOIN,Restrictions.eqProperty("buildOpcoSetting.opco.id", "testcycleOpcoSetting.opco.id"));

An Access problem: Not getting required output while building a report

I have created a report in Access and I have written a query for fetching records from
multiple tables as follows:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM Contractors
INNER JOIN (BuildingDetails
INNER JOIN (ActionDetails
INNER JOIN DormData ON ActionDetails.ActionID = DormData.ActionID)
ON BuildingDetails.BuildingID = DormData.BuildingID)
ON Contractors.ID = DormData.ItemID;
Now what I want is only actiontype=repair or actionid=1 get retrieved by the query. We have two actontype "repair" and "replace".
I have reformatted you query a little to neaten it up. You haven't specified what the data looks like for the filter but based on what you have said I would go with something like the following
SELECT BuildingDetails.*,
Contractors.Item,
ActionDetails.ActionType
FROM Contractors
INNER JOIN DormData ON Contractors.ID = DormData.ItemID
INNER JOIN ActionDetails ON DormData.ActionID = ActionDetails.ActionID
INNER JOIN BuildingDetails ON DormData.BuildingID = BuildingDetails.BuildingID
WHERE ActionDetails.ActionType = 'Repair' OR ActionID=1
If ActionID is a lookup column that relates ActionID(1) to ActionType ('Repair') then you don't need the or and can stick to one or other of the conditions in the WHERE Clause.
Hope this helps.
I suspect you only need to filter using actiontype = 'repair' (I further guess that ActionID is an autonumber and you have a row {ActionID = 1, actiontype = 'repair'} only by chance... but this is maybe extrapolating too far :)
I'm surprised #David Steele's answer works in Access (ACE, Jet, whatever) because he's removed the parentheses from the JOIN clauses (however if it does -- suggesting a linked table -- then you should "accept" that answer). But I too could resist 'neatening them up' so that the ON clauses are close to the table names:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM ((DormData
INNER JOIN Contractors
ON Contractors.ID = DormData.ItemID)
INNER JOIN BuildingDetails
ON BuildingDetails.BuildingID = DormData.BuildingID)
INNER JOIN ActionDetails
ON ActionDetails.ActionID = DormData.ActionID
WHERE ActionDetails.ActionType = 'repair';
Add this to the end of your select statement to fix the issue:
where actiondetails.actiontype = 'repair' or actiondetails.actionid = 1