postgresql: join syntax error when joining 2 select statements - sql

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;

Related

How to fix SQL query to Left Join a subquery with Where clause?

I'm new to SQL and I'm not certain why I am getting this error. I am trying to left join a sub-query to another query in sql developer.
This is the first query,
SELECT DISTINCT
tl.species,
ag.age
FROM
age_list ag,
tree_list tl
WHERE
ag.tree_id = tl.tree_id
And then the sub-query I would like to left join where the tree_id = tree_number is,
SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE
sl.tree_spon < 10
When trying to do this I've tried to use,
SELECT DISTINCT
tl.species,
ag.age,
q1.tree_spon
FROM
age_list ag,
tree_list tl
LEFT OUTER JOIN (SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE sl.tree_spon < 10) q1 on q1.tree_number = tree_list.tree_id
WHERE
ag.tree_id = tl.tree_id
Whatever I change in terms of the alias' for the columns and tables I always get the error, "ORA-00904: invalid identifier error", and that "tree_list.tree_id is invalid identifier", though separately the queries run fine.
Can anyone help, is it an issue with both queries joining on the tl.tree_id?
You can use the ANSI join syntax throughout (rather than mixing in legacy comma joins), joining on ag.tree_id = sl.tree_number (or tl.tree_id = sl.tree_number but they're both equal given the previous join) and putting the filter on sl.tree_spon < 10 into the ON clause as well:
SELECT DISTINCT
tl.species,
ag.age,
sl.tree_spon,
sl.tree_number
FROM age_list ag
INNER JOIN tree_list tl
ON (ag.tree_id = tl.tree_id)
LEFT OUTER JOIN spon_list sl
ON (ag.tree_id = sl.tree_number AND sl.tree_spon < 10)
Change tree_list.tree_id to tl.tree_id

Joining a Query and sub-query

I'm trying to join a query and a sub-query in Access but haven't the faintest idea on how to do that. Rather than saving the sub-query as a different query and then joining it to the main query.
Main query (with reference to sub-query):
SELECT tb200_IraDataIn.tb200_niarnum, tb206_IraAccount.tb206_IraAccDesc,
tb206_IraAccount.tb206_IraAccNum, tb15_Securities.tb15_IsActiveRegister,
tb15_Securities.tb15_NiarDesc, tb15_Securities.tb15_ManpikID, tb200_IraDataIn.tb200_Shovi, tb10_Afik.tb10_InvestTool,
tb206_IraAccount.tb206_IsActive, tb200_IraDataIn.tb200_Shovi,
tb200_IraDataIn.tb200_Shovi/SubQuery1.SumOftb200_Shovi AS Expr1
FROM SubQuery1
INNER JOIN (tb10_Afik
INNER JOIN (tb15_Securities
INNER JOIN (tb206_IraAccount
INNER JOIN tb200_IraDataIn
ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber)
ON tb15_Securities.tb15_NiarID = tb200_IraDataIn.tb200_niarnum)
ON (tb10_Afik.tb10_AfikID = tb200_IraDataIn.tb200_afik) AND (tb10_Afik.tb10_Erp =tb200_IraDataIn.tb200_ERP))
ON SubQuery1.tb206_IraAccNum = tb206_IraAccount.tb206_IraAccNum
WHERE (((tb15_Securities.tb15_IsActiveRegister)=Yes) AND ((tb10_Afik.tb10_InvestTool)=1
Or (tb10_Afik.tb10_InvestTool)=4 Or (tb10_Afik.tb10_InvestTool)=21 Or
(tb10_Afik.tb10_InvestTool)=3) AND ((tb206_IraAccount.tb206_IsActive)=Yes) AND
(([tb200_IraDataIn].[tb200_Shovi]/[SubQuery1].[SumOftb200_Shovi])>0.1));
subquery (saved as SubQuery1):
SELECT tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum, Sum(tb200_IraDataIn.tb200_Shovi) AS SumOftb200_Shovi
FROM tb206_IraAccount
INNER JOIN tb200_IraDataIn ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber
WHERE (((tb206_IraAccount.tb206_IsActive)=Yes))
GROUP BY tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum;
How can I put the sql statement of the sub-query inside the same Sql statement of the main query, unlike now?
Thanks!
Nest between parentheses. Nothing else in the main query changes.
SELECT
...
FROM (SELECT tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum, Sum(tb200_IraDataIn.tb200_Shovi) AS SumOftb200_Shovi
FROM tb206_IraAccount
INNER JOIN tb200_IraDataIn ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber
WHERE (((tb206_IraAccount.tb206_IsActive)=Yes))
GROUP BY tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum) AS SubQuery1
...

HQL 'With statement' cause an additional join

The HQL With statement produce strange behavior. I expect the following query:
select
s.id, s.nom,s.dateDebutPublication,s.dateFinPublication,r.codeUtilisateur
from
Sondage s left outer join
s.repondant r with r.codeUtilisateur=:utilisateur
to produce this sql request:
select ...
from
SON_SONDG sondage0_
left outer join
SON_REPND repondant1_
on sondage0_.SEQ_SONDG=repondant1_.SEQ_SONDG
and (
repondant1_.CODE_UTILS=?
)
instead, it create an extra join, which return way more lines
left outer join
SON_REPND repondant1_
on sondage0_.SEQ_SONDG=repondant1_.SEQ_SONDG
left outer join
SON_REPND repondant2_
on repondant1_.SEQ_REPND=repondant2_.SEQ_REPND
and (
repondant2_.CODE_UTILS=?
)
what would be the correct way to write de HQL query so it return my expected sql query?

Having trouble with microsoft query syntax

I wrote a SQL query that works, but when I try to translate it to query on Microsoft query I'm running into a lot of problems syntactically.
This is the query I wrote originally:
select
sum(PJC_FCTR_VL)
from
(select
max(PJC_FCTR_VL)
from
(select
R_CATEGORY_70075,
PRDC_KEY
from PRDC_DIM
where R_CATEGORY_70075 like '%RAMEN%')sub,
HS_PNLS_PJC_SBST_20150404 t1,
HSHLD_DIM t2,
HS_PRCH_SBST_20150404 t3
where
t1.PNLS_TYP_ID = 1
and t3.HSHLD_ID = t2.HSHLD_ID
and sub.PRDC_KEY = t3.PRDC_KEY
group by
t1.HSHLD_ID) subqry
I tried to break this query down more to see where I'm messing up on the Microsoft query and this is what I've got:
select
max(HS_PNLS_PJC_SBST_20150404.PJC_FCTR_VL)
from
{
PRDC_DIM.R_CATEGORY_70075, PRDC_DIM.PRDC_KEY
FROM AODR2QPNLSFWPR.ADMIN.PRDC_DIM PRDC_DIM
WHERE (PRDC_DIM.R_CATEGORY_70075 Like '%RAMEN%')} HS_PNLS_PJC_SBST_20150404
I end up getting an error that says "expected OJ after {" even though I'm not really doing an outer join. I was wondering if someone could help me edit the SQL so that it would satisfy the Microsoft syntax.
Try something like.....
select sum(Max_PJC_FCTR_VL)
from
( select t1.HSHLD_ID, max(PJC_FCTR_VL) AS Max_PJC_FCTR_VL --<-- use two part name here
from ( select R_CATEGORY_70075,
PRDC_KEY
from PRDC_DIM
where R_CATEGORY_70075 like '%RAMEN%'
)sub
INNER JOIN HS_PRCH_SBST_20150404 t3 ON sub.PRDC_KEY = t3.PRDC_KEY
INNER JOIN HSHLD_DIM t2 ON t3.HSHLD_ID = t2.HSHLD_ID
INNER JOIN HS_PNLS_PJC_SBST_20150404 t1 ON t1.HSHLD_ID = t2.HSHLD_ID --<-- only guessing here
AND t1.PNLS_TYP_ID = 1
group by t1.HSHLD_ID
) subqry

Error on an ANSI SQL Statement

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'