Error on an ANSI SQL Statement - sql

select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from capd_a t1
right outer join capd_b t2
on (t2.s_id = t1.p_id)
join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))
I receive this error:
Syntax Error (Missing Operator) on (t2.s_id = t1.p_id)
join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))
Any idea what is wrong with my query?

Access has a pervert version of SQL and not the ISO/ANSI standard. It really likes (read: requires) parenthesis when joining more than two tables. There are also restrictions on what types of joins (LEFT, RIGHT, INNER) are allowed inside parentheses:
from
( --- required
capd_a t1
INNER join capd_b t2 --- RIGHT join turned into INNER
on (t2.s_id = t1.p_id)
) --- required
INNER join capd_c t3
on ((t3.e_student=t1.p_id) and (t3.e_reference='D /YR2A2/12'))

Try building your query in the Access query designer. Coming from a different database system, you may not like the query designer much, but I'm suggesting you try it anyway because it will help you create SQL which the Access db engine can accept.
Among the differences you're facing are:
Access requires parentheses when your query includes more that 2 data sources, and is very demanding about their positions.
The db engine does not recognize JOIN as a synonym for INNER JOIN. You must always specify the join type (INNER; LEFT; or RIGHT).
The engine will accept RIGHT OUTER JOIN, but the query designer will change it to just RIGHT JOIN. So it doesn't really make a difference, but I mentioned it only to avoid confusion.
If you can't tolerate the query designer, I'll offer this as a starting point.
select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from (capd_a t1
inner join capd_b t2
on t2.s_id = t1.p_id)
inner join capd_c t3
on t3.e_student=t1.p_id
where t3.e_reference='D /YR2A2/12'
I user inner join both times because in certain situations Access balks with LEFT or RIGHT joins and INNER joins in the same query. I'm uncertain whether your RIGHT join would be a problem in this case, but both as INNER will surely work. So first get all the rest of the query syntax working correctly, then revise your join types as needed.
I moved t3.e_reference='D /YR2A2/12' from the ON to a WHERE clause. That's my preference, but I don't think it will make a difference either way.

As your query includes t3.e_reference='D /YR22/A12' in ON statement scope which should be excluded from ON statement scope and write it seperately outside the ON statement scope......
You can do like this;
select t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
from capd_a t1
right outer join capd_b t2
on (t2.s_id = t1.p_id)
join capd_c t3
on (t3.e_student=t1.p_id)
and (t3.e_reference='D /YR2A2/12')
Or, you can do use WHERE clause in query:
SELECT t2.s_studentreference
, t1.p_surname
, t1.p_forenames
, t3.e_reference
, t3.e_name
FROM capd_a t1
RIGHT OUTER JOIN capd_b t2
ON (t2.s_id = t1.p_id)
JOIN capd_c t3
ON (t3.e_student = t1.p_id)
WHERE t3.e_reference='D /YR2A2/12'

Related

LEFT JOIN expression not supported

I need to do a query with a left outer join just like below, however Access is showing a warning dialog "JOIN expression not supported".I understand that Access doesn't support INNER JOIN nested inside a LEFT JOIN but as I am a beginner in SQL I don't see any other way to get the same result.
The goal of the query is to get everything that is in the select even when InvoiceItems.Amount is null.
SELECT MainOrder.OrderNumber, OrderComponent.ArticleNumber, SupplierOrderMain.*, InvoiceItems.Amount
FROM InvoiceItems LEFT JOIN
((MainOrder INNER JOIN
OrderComponent
ON MainOrder.OrderNumber = OrderComponent.OrderNumber
) INNER JOIN
SupplierOrderMain
ON OrderComponent.ID = SupplierOrderMain.OrderComponentID
)
ON InvoiceItems.OrderComponent = OrderComponent.ID;
I'm not sure why you would want outer joins in this situation at all (you don't explain why). But just start with the table where you want to keep everything and work from there:
SELECT MainOrder.OrderNumber, OrderComponent.ArticleNumber, SupplierOrderMain.*, InvoiceItems.Amount
FROM ((InvoiceItems LEFT JOIN
OrderComponent
ON InvoiceItems.OrderComponent = OrderComponent.ID
) LEFT JOIN
MainOrder
ON MainOrder.OrderNumber = OrderComponent.OrderNumber
) LEFT JOIN
SupplierOrderMain
ON OrderComponent.ID = SupplierOrderMain.OrderComponentID

postgresql: join syntax error when joining 2 select statements

I am querying the aact database from clinicaltrials.gov. The database model is right here: https://aact.ctti-clinicaltrials.org/schema. I have two schemas that I am selecting from (ctgov, proj_cdek_standard_orgs). I am trying to join two select statements. edit: I have now tried aliasing my subqueries, but that still does nothing. I get the following error:
(SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors, ctgov.studies
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id) A
FULL [OUTER] JOIN
(SELECT proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms) B
ON
A.name = B.name;
I can do both select statements perfectly fine on their own, but I try the query and I get this error:
ERROR: syntax error at or near "t1" LINE 7: ) t1
What did I do wrong and how do I use joins without getting syntax errors?
Please use below query,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors, ctgov.studies, proj_cdek_standard_orgs.cdek_synonyms
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id
and proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name;
But the right way is to use traditional joins,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors
INNER JOIN ctgov.studies
ON (ctgov.sponsors.nct_id=ctgov.studies.nct_id)
INNER JOIN proj_cdek_standard_orgs.cdek_synonyms
ON (proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name);
You can change it to LEFT or FULL OUTER JOIN according to your requirement.
You have to provide an alias to the sub-queries. Also you should not use implicit joins as you have used in first subquery, always try to use explicit joins.
SELECT
*
FROM
(
SELECT
ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors
JOIN ctgov.studies
ON ctgov.sponsors.nct_id=ctgov.studies.nct_id
) t1
FULL JOIN
(
SELECT
proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms
) t2
ON
t1.name = t2.name;

"Join Expression Not Supported" - Need help on Microsoft Access Query

I need some help making the following SQL code work:
SELECT MemberData.MemberID, Transaction.TrxID, Transaction.Date,
Transaction.GroupID, TransactionDetails.CashIn, TransactionDetails.CashOut
FROM [Transaction]
INNER JOIN (MemberData LEFT JOIN TransactionDetails
ON MemberData.[MemberID] = TransactionDetails.[MemberID])
ON Transaction.[TrxID] = TransactionDetails.[TrxID];
An error stating below keeps popping up:
Join Expression Not Supported"
What am I doing wrong and how can I fix this?
I can't pinpoint the exact source of the error, but a simple fix for most of the Join expression not supported errors is to do one join at a time, and use subqueries.
SELECT MemberData.MemberID, T.TrxID, T.[Date],
T.GroupID, T.CashIn, T.CashOut
FROM (
SELECT [Transaction].TrxID, [Transaction].Date, [Transaction].GroupID, TransactionDetails.CashIn, TransactionDetails.CashOut, TransactionDetails.[MemberID]
FROM [Transaction]
LEFT JOIN TransactionDetails ON [Transaction].[TrxID] = TransactionDetails.[TrxID]
) AS T
INNER JOIN MemberData ON MemberData.[MemberID] = T.[MemberID];
#Poomind #Erik von Asmuth Just change the INNER JOIN into RIGHT JOIN You could try pasting the next SQL-statement into a new query.
SELECT MemberData.MemberID, T.TrxID, T.[Date], T.GroupID, T.CashIn, T.CashOut
FROM (SELECT [Transaction].TrxID, [Transaction].Date, [Transaction].GroupID,
TransactionDetails.CashIn, TransactionDetails.CashOut, TransactionDetails.[MemberID]
FROM [Transaction]
LEFT JOIN TransactionDetails ON [Transaction].[TrxID] = TransactionDetails.[TrxID])
AS T RIGHT JOIN MemberData ON MemberData.[MemberID] = T.[MemberID];
When I saved this query in a sample database, ms-access (version 2003) changed it to the following SQL-statement. So, don't worry if this happens with you too.
SELECT MemberData.MemberID, T.TrxID, T.[Date], T.GroupID, T.CashIn, T.CashOut
FROM [SELECT [Transaction].TrxID, [Transaction].Date, [Transaction].GroupID, TransactionDetails.CashIn, TransactionDetails.CashOut, TransactionDetails.[MemberID]
FROM [Transaction]
LEFT JOIN TransactionDetails ON [Transaction].[TrxID] = TransactionDetails.[TrxID]
]. AS T RIGHT JOIN MemberData ON MemberData.[MemberID] = T.[MemberID];

LEFT JOIN ON COALESCE(a, b, c) - very strange behavior

I have encountered very strange behavior of my query and I wasted a lot of time to understand what causes it, in vane. So I am asking for your help.
SELECT count(*) FROM main_table
LEFT JOIN front_table ON front_table.pk = main_table.fk_front_table
LEFT JOIN info_table ON info_table.pk = front_table.fk_info_table
LEFT JOIN key_table ON key_table.pk = COALESCE(info_table.fk_key_table, front_table.fk_key_table_1, front_table.fk_key_table_2)
LEFT JOIN side_table ON side_table.fk_front_table = front_table.pk
WHERE side_table.pk = (SELECT MAX(pk) FROM side_table WHERE fk_front_table = front_table.pk)
OR side_table.pk IS NULL
Seems like a simple join query, with coalesce, I've used this technique before(not too many times) and it worked right.
In this query I don't ever get nulls for side_table.pk. If I remove coalesce or just don't use key_table, then the query returns rows with many null side_table.pk, but if I add coalesce join, I can't get those nulls.
It seems key_table and side_table don't have anything in common, but the result is so weird.
Also, when I don't use side_table and WHERE clause, the count(*) result with coalesce and without differs, but I can't see any pattern in rows missing, it seems random!
Real query:
SELECT ECHANGE.EXC_AUTO_KEY, STOCK_RESERVATIONS.STR_AUTO_KEY FROM EXCHANGE
LEFT JOIN WO_BOM ON WO_BOM.WOB_AUTO_KEY = EXCHANGE.WOB_AUTO_KEY
LEFT JOIN VIEW_WO_SUB ON VIEW_WO_SUB.WOO_AUTO_KEY = WO_BOM.WOO_AUTO_KEY
LEFT JOIN STOCK stock3 ON stock3.STM_AUTO_KEY = EXCHANGE.STM_AUTO_KEY
LEFT JOIN STOCK stock2 ON stock2.STM_AUTO_KEY = EXCHANGE.ORIG_STM
LEFT JOIN CONSIGNMENT_CODES con2 ON con2.CNC_AUTO_KEY = stock2.CNC_AUTO_KEY
LEFT JOIN CONSIGNMENT_CODES con3 ON con3.CNC_AUTO_KEY = stock3.CNC_AUTO_KEY
LEFT JOIN CI_UTL ON CI_UTL.CUT_AUTO_KEY = EXCHANGE.CUT_AUTO_KEY
LEFT JOIN PART_CONDITION_CODES pcc2 ON pcc2.PCC_AUTO_KEY = stock2.PCC_AUTO_KEY
LEFT JOIN PART_CONDITION_CODES pcc3 ON pcc3.PCC_AUTO_KEY = stock3.PCC_AUTO_KEY
LEFT JOIN STOCK_RESERVATIONS ON STOCK_RESERVATIONS.STM_AUTO_KEY = stock3.STM_AUTO_KEY
LEFT JOIN WAREHOUSE wh2 ON wh2.WHS_AUTO_KEY = stock2.WHS_ORIGINAL
LEFT JOIN SM_HISTORY ON (SM_HISTORY.STM_AUTO_KEY = EXCHANGE.ORIG_STM AND SM_HISTORY.WOB_REF = EXCHANGE.WOB_AUTO_KEY)
LEFT JOIN RC_DETAIL ON stock3.RCD_AUTO_KEY = RC_DETAIL.RCD_AUTO_KEY
LEFT JOIN RC_HEADER ON RC_HEADER.RCH_AUTO_KEY = RC_DETAIL.RCH_AUTO_KEY
LEFT JOIN WAREHOUSE wh3 ON wh3.WHS_AUTO_KEY = COALESCE(RC_DETAIL.WHS_AUTO_KEY, stock3.WHS_ORIGINAL, stock3.WHS_AUTO_KEY)
WHERE STOCK_RESERVATIONS.STR_AUTO_KEY = (SELECT MAX(STR_AUTO_KEY) FROM STOCK_RESERVATIONS WHERE STM_AUTO_KEY = stock3.STM_AUTO_KEY)
OR STOCK_RESERVATIONS.STR_AUTO_KEY IS NULL
Removing LEFT JOIN WAREHOUSE wh3 gives me about unique EXC_AUTO_KEY values with a lot of NULL STR_AUTO_KEY, while leaving this row removes all NULL STR_AUTO_KEY.
I recreated simple tables with numbers with the same structure and query works without any problems o.0
I have a feeling COALESCE is acting as a REQUIRED flag for the joined table, hence shooting the LEFT JOIN to become an INNER JOIN.
Try this:
SELECT COUNT(*)
FROM main_table
LEFT JOIN front_table ON front_table.pk = main_table.fk_front_table
LEFT JOIN info_table ON info_table.pk = front_table.fk_info_table
LEFT JOIN key_table ON key_table.pk = NVL(info_table.fk_key_table, NVL(front_table.fk_key_table_1, front_table.fk_key_table_2))
LEFT JOIN (SELECT fk_, MAX(pk) as pk FROM side_table GROUP BY fk_) st ON st.fk_ = front_table.pk
NVL might behave just the same though...
I undertood what was the problem (not entirely though): there is a LEFT JOIN VIEW_WO_SUB in original query, 3rd line. It causes this query to act in a weird way.
When I replaced the view with the other table which contained the information I needed, the query started returning right results.
Basically, with this view join, NVL, COALESCE or CASE join with combination of certain arguments did not work along with OR clause in WHERE subquery, all rest was fine. ALthough, I could get the query to work with this view join, by changing the order of joined tables, I had to place table participating in where subquery to the bottom.

Postgresql - Having trouble performing left joins on multiple tables

I'm in the process of moving some Mysql queries over to Postgresql and I ran across this one that doesn't work.
select (tons of stuff)
from trip_publication
left join trip_collection AS "tc" on
tc.id = tp.collection_id
left join
trip_author ta1, (dies here)
trip_person tp1,
trip_institution tai1,
trip_location tail1,
trip_rank tr1
ON
tp.id = ta1.publication_id
AND tp1.id = ta1.person_id
AND ta1.order = 1
AND tai1.id = ta1.institution_id
AND tail1.id = tai1.location_id
AND ta1.rank_id = tr1.id
The query seems to be dying on the "trip_author ta1" line, where I marked it above. The actual error message is:
syntax error at or near ","
LINE 77: (trip_author ta1, trip_person tp1, ...
I went through the docs, and it seems to be correct. What exactly am I doing wrong here? Any feedback would be much appreciated.
I don't know postgres, but in regular SQL you would need to a series of LEFT JOIN statements rather than your comma syntax. You seemed to have started this then stopped after the first two.
Something like:
SELECT * FROM
table1
LEFT JOIN table2 ON match1
LEFT JOIN table3 ON match2
WHERE otherFilters
The alternative is the older SQL syntax of:
SELECT cols
FROM table1, table2, table3
WHERE match AND match2 AND otherFilters
There's a couple of other smaller errors in your SQL, like the fact you forgot your tp alias on your first table, and have tried including a where clause (ta1.order = 1) as a joining constraint.
I think this is what you are after:
select (tons of stuff)
from trip_publication tp
left join trip_collection AS "tc" on tc.id = tp.collection_id
left join trip_author ta1 on ta1.publication_id = tp.id
left join trip_person tp1 on tp1.id = ta1.person_id
left join trip_institution tai1 on tai1.id = ta1.institution_id
left join trip_location tail1 on tail1.id = tai1.location_id
left join trip_rank tr1 on tr1.id = ta1.rank_id
where ta1.order = 1
Your left joins are one per table you are joining
left join trip_author ta1 on ....
left join trip_person tp1 on ....
left join trip_institution on ...
...and so on