简体   繁体   中英

Oracle SQL multiple tables

I'm still pretty fresh with SQL in general so this is probably not the best worded question. I want to get columns from different tables. They don't have any common column but there's 3rd table that has information that's able to link them together.

This is how I've tried to go about it:

SELECT 
    CI_SA.ACCT_ID,
    CI_SA.EXPIRE_DT WHERE EXPIRE_DT BETWEEN '15-APR-17' AND '30-APR-17',
    CI_SP.SP_TYPE_CD WHERE SP_TYPE_CD='G-RES'
FROM CI_SA
INNER JOIN CI_SA ON CI_SA.SA_ID = CI_SA_SP.SA_ID AND CI_SP.SP_ID =  
CI_SA_SP.SP_ID

And this is the error that I receive in return: ORA-00923: FROM keyword not found where expected 00923. 00000 - "FROM keyword not found where expected" *Cause:
*Action: Error at Line: 3 Column: 18

Anyone would be able to point me where I've got it wrong?

the where condition should be after from table for example select * from tab1 where ....

try it like this :

SELECT 
    CI_SA.ACCT_ID,
    CI_SA.EXPIRE_DT ,
    CI_SP.SP_TYPE_CD 
FROM CI_SA
INNER JOIN CI_SA ON CI_SA.SA_ID = CI_SA_SP.SA_ID AND CI_SP.SP_ID =  
CI_SA_SP.SP_ID
WHERE EXPIRE_DT BETWEEN '15-APR-17' AND '30-APR-17'
and SP_TYPE_CD='G-RES'

also as user artur notices , are you joinin on CI_SA ? or CI_SA_SP ? if CI_SA you have too add alias like the below:

INNER JOIN CI_SA CI_SA_SP ...

The where clauses should be at the end:

SELECT 
    CI_SA.ACCT_ID,
    CI_SA.EXPIRE_DT ,
    CI_SP.SP_TYPE_CD 
FROM CI_SA
INNER JOIN CI_SA ON CI_SA.SA_ID = CI_SA_SP.SA_ID AND CI_SP.SP_ID =  
CI_SA_SP.SP_ID
WHERE EXPIRE_DT BETWEEN '15-APR-17' AND '30-APR-17' and SP_TYPE_CD='G-RES'

The WHERE clause goes after the FROM and only appears once per SELECT .

Perhaps you intend:

SELECT CI_SA.ACCT_ID, CI_SA.EXPIRE_DT, CI_SP.SP_TYPE_CD 
FROM CI_SA INNER JOIN
     CI_SA_SP
     ON CI_SA.SA_ID = CI_SA_SP.SA_ID AND
        CI_SP.SP_ID = CI_SA_SP.SP_ID
WHERE CI_SA.EXPIRE_DT BETWEEN DATE '2017-04-15' AND DATE '2017-04-30' AND
      CI_SP.SP_TYPE_CD = 'G-RES';

In addition, this fixes the FROM clause so it refers to CI_SA_SP . I assume this was a typo in your original query. Also, I strongly recommend using ISO-standard date formats, so this uses the DATE keyword.

First 'SELECT' then, 'FROM' and the last 'WHERE'. Only one 'WHERE' clause is allowed. Duplicated CI_SA in join. You should use aliases

In addition to what the others have said, re. the where clauses being in the wrong place, you also need to join your tables correctly. I think you're after something like:

SELECT CI_SA.ACCT_ID,
       CI_SA.EXPIRE_DT WHERE EXPIRE_DT BETWEEN '15-APR-17' AND '30-APR-17',
       CI_SP.SP_TYPE_CD WHERE SP_TYPE_CD='G-RES'
FROM   CI_SA
       INNER JOIN CI_SA_SP ON CI_SA.SA_ID = CI_SA_SP.SA_ID 
       INNER JOIN CI_SP ON CI_SP.SP_ID = CI_SA_SP.SP_ID
WHERE  CI_SA.EXPIRE_DT BETWEEN TO_DATE('01/04/2017', 'DD/MM/YYYY') AND TO_DATE('30/04/2017', 'DD/MM/YYYY')
AND    CI_SP.SP_TYPE_CD = 'G-RES';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM