Oracle Query Error ORA-00933 SQL Command not properly ended - sql

Trying to run a simple inner join in a query on a 5.1 Oracle Database.
SELECT
W.ID,
WE.CLASS
FROM
W
INNER JOIN
WE
ON (W.ID=WO.ID)
WHERE
WO.ID='688158'
It results in the Command not properly ended.
I can do
Select
W.ID, WE.CLASS from W, WE
WHERE
W.ID=WO.ID and WO.ID='688158'
and it doesn't error out but doesn't return the results I want because of the lack of join.
Thanks!

Your table name is used incorrectly. Should be:
SELECT
W.ID,
WE.CLASS
FROM
W
INNER JOIN
WE
ON (W.ID = WE.ID)
WHERE
WE.ID='688158';
WE instead of WO

Related

Databricks - Correlated scalar sub-queries can only be used in a Filter/Aggregate/Project

I trying to execute the following code in databricks but I receive the error: Error in SQL statement: AnalysisException: Correlated scalar sub-queries can only be used in a Filter/Aggregate/Project and a few commands: Join LeftOuter
SELECT
M.VAR_1,
C.VAR_2,
N.VAR_3
FROM TABLE_1 C
INNER JOIN TABLE_2 M ON C.CONTRACT_ID = M.CONTRACT_ID
INNER JOIN TABLE_3 FINREC ON M.CODE_2 = FINREC.CODE_3
LEFT JOIN TABLE_4 N ON N.CODE_4 = (SELECT MAX(NN.CODE_4)
FROM TABLE_4 NN
WHERE NN.CODE_5 = M.CODE_5)
The query I am trying in databricks works fine in Oracle and I cannot understand what's the error I get and nor how to solve it. I am pretty new to databricks though

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;

Multiple join with SQL

I have a problem with my SQL query.
I want to have a multiple join, but the error is not helpful.
The following is my query:
SELECT bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client
,BN_CS_MP_MASTERPROFILE.CMP_NAME AS Nom_prenom
,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster
INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE = BN_CS_MP_MASTERPROFILE.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE = PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE
This query give back to me the flowing error:
Impossible to add table bn_ms_bm_bankmaster with Microsoft Query
Any help will welcome.
Can you try with below.
select * from(Select bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client,BN_CS_MP_MASTERPROFILE.CMP_NAME as Nom_prenom,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE FROM bn_ms_bm_bankmaster INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE=BN_CS_MP_MASTERPROFILE.CMP_CUSTCODEINNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE=PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE);
Please try this.
Select A.CMP_CUSTCODE AS Id_client,
B.CMP_NAME as Nom_prenom,
C.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster AS A
INNER JOIN BN_CS_MP_MASTERPROFILE AS B ON A.CMP_CUSTCODE= B.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS AS C ON B.CMP_CUSTCODE = C.CMP_CUSTCODE

JOIN Nested DISTINCT

I am writing SQL statements with MS Access and I am wondering how I can nest a SELECT DISTINCT statement in my JOIN?
At the moment I am writing 2 queries to get an output (2 steps):
1st query is a simple DISTINCT statement.
2nd query is a JOIN on the query created in 1.
How can I nest a DISTINCT statement in the join in order to perform the action in a single step?
SELECT DISTINCT tblFinalIssuerNames_ReverseRepos.ISIN, tblFinalIssuerNames_ReverseRepos.IssuerCode
FROM tblFinalIssuerNames_ReverseRepos;
SELECT QSel_CollateralReposII.ISIN, QSel_CollateralReposII.MarketValueUSD, QSelDistinctISINs.IssuerCode
FROM QSel_CollateralReposII INNER JOIN QSelDistinctISINs ON QSel_CollateralReposII.ISIN = QSelDistinctISINs.ISIN;
I was thinking about something like the below but the syntax is wrong…
SELECT QSel_CollateralReposII.ISIN, QSel_CollateralReposII.MarketValueUSD, tblFinalIssuerNames_ReverseRepos.IssuerCode
FROM QSel_CollateralReposII INNER JOIN tblFinalIssuerNames_ReverseRepos ON SELECT DISTINCT QSel_CollateralReposII.ISIN = tblFinalIssuerNames_ReverseRepos.ISIN;
I can't test what is allowed and what is not in Access but you can replace QSelDistinctISINs in your query with the query that defines it, by just putting it inside parenthesis and giving it an alias - d below. This is valid SQL syntax:
SELECT
r.ISIN,
r.MarketValueUSD,
d.IssuerCode
FROM
QSel_CollateralReposII r
INNER JOIN
( SELECT DISTINCT
ISIN,
IssuerCode
FROM
tblFinalIssuerNames_ReverseRepos
) d
ON r.ISIN = d.ISIN ;

SQL query works in SQL Server, fails in Excel (Microsoft Query)

I have the following query which works as intended :
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS maxDate, CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The query is to gives me the latest (max) service history date for a given customer.
When I execute the query in SQL Server, it works perfectly fine, but when I put the same query into EXCEL 2010 (Microsoft Query) it give me the error:
No Column name was specified for Column 1 of 'testQuery'
Invalid column name 'maxDate'
Statement could not be prepared
I'm not able to fix the query to get pass the error. Can someone please tell me why Excel isn't working with the above query? Thanks
You need to put testQuery and maxDate inside single quotations
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS 'maxDate', CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS 'testQuery' ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The only thing you need to do is to add square brackets around the maxDate like following:
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS [maxDate], CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate