Several ON clause in JOIN doesn't work properly in Access - sql

The problem is really weird. The code below gives the error, that there is unsupportable JOIN statement
update ((s2t
left join tables on
S2T.[table] = Tables.TableName)
left join Columns on
(S2T.[column] = Columns.ColumnName))
left join s2t_source on
( s2t_source.source_table = "money")
set s2t.source_id = 1;
But then I changed join conditions on third JOIN this way and it started to work:
update ((s2t
left join tables on
S2T.[table] = Tables.TableName)
left join Columns on
(S2T.[column] = Columns.ColumnName))
inner join s2t_source on
( s2t_source.source_table = S2T.[table])
set s2t.source_id = 1;
What am I doing wrong? Firstly, I thought that problem related with ON clauses, that use info from just previously joined table. However then I found similar code, which seems to be work. I'm confused. How is that possible, that changing ON clause makes the query to fail?
upd1: I don't think that the problem related to datatype.
Text datatype: s2t_source.source_table, s2t_source.source_field, S2T.[table], S2T.[column], Tables.TableName, Columns.ColumnName
Integer datatype: Tables.id, Columns.id, s2t.source_id
upd2: I'm using Access 2010. Among this 3 tables, there are only 1 foreign key between tables named Tables and Columns
Tables.id = Columns.table_id

s2t_source.source_table = "money"
...is not a JOIN Condition but a WHERE Condition. The right hand site variable should contain something with the previous tables/joins.
Not knowing the context, I can only guess a solution:
update ((s2t
left join tables on
S2T.[table] = Tables.TableName)
left join Columns on
(S2T.[column] = Columns.ColumnName))
left join s2t_source on
( s2t_source.source_table = S2T.[table])
set s2t.source_id = 1
WHERE s2t_source.source_table = "money";

Related

How do i perform a conditional update in SQL with data from multiple tables?

Trying to run an update on a column and i'm getting a syntax error on the FROM line connecting the tables
UPDATE inv_loc
SET inv_loc.product_group_id = 'TEMP'
WHERE inv_mast_ud.eh_spk LIKE '%T'
FROM
inv_mast_ud
left join inv_loc on inv_mast_ud.inv_mast_uid = inv_loc.inv_mast_uid
WHERE comes after FROM. I think you want:
UPDATE inv_loc
SET inv_loc.product_group_id = 'TEMP'
FROM inv_loc JOIN
inv_mast_ud
ON inv_mast_ud.inv_mast_uid = inv_loc.inv_mast_uid
WHERE inv_mast_ud.eh_spk LIKE '%T';
Note that I changed the LEFT JOIN to an INNER JOIN. You are updating inv_loc, so it makes no sense that that table is the second table in a LEFT JOIN.
I assume you actually want to filter the rows, so a LEFT JOIN is not needed. Otherwise, you would not need inv_mast_ud.
The WHERE clause belongs at the end of the update join statement:
UPDATE il
SET il.product_group_id = 'TEMP'
FROM inv_loc il
INNER JOIN inv_mast_ud imu
ON imu.inv_mast_uid = il.inv_mast_uid
WHERE
imu.eh_spk LIKE '%T';
See SQL update query using joins for a good canonical answer to your question.

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)

Join expression not supported 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.

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

Basic SQL join question. Can you help me improve my skillset?

Ok.. So I'm trying to improve my SQL skills and have a question. Here is a screen shot of the schema.
Schema http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif
(http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif)
Alright so I'm selecting a bunch of Report Bundles and other rows from a table you can see. I've got these two tables joining together correctly and displaying what should be returned. Now I need to add another field onto my result rows that states what type of report this is. How can I join up to the ReportGroupType table through the ReportBundleGroup table without getting a shwack of results?
Here is the query I am using so far.
SELECT *
FROM ReportBundleCustomerVisibility INNER JOIN ReportBundle
ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
Thanks again SO
SELECT *
FROM ReportBundleCustomerVisibility AS v
JOIN ReportBundle AS b ON b.ID = v.ReportBundleID
JOIN ReportBundleGroup AS g ON b.ID = g.ReportBundleID
JOIN ReportGroupTYpe AS t ON t.ID = g.ReportGroupTypeID
WHERE v.ReferenceCustomerID = 2303
It sounds like you just need another inner join to get the information you need. You can think about the second join as joining the result of the join with the ReportGroupType table. I added parenthesis to try to join the two sets the second INNER JOIN is operating on.
SELECT * FROM ((ReportBundleCustomerVisibility
INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID)
INNER JOIN ReportGroupType ON ReportBundleGroup.ReportGroupTypeID = ReportGroupType.ID)
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
I also highly suggest against using "SELECT *" in production code or any query you plan on reusing as the table schema can change and possibly effect reports and UI. Explicitly specify the columns instead.