codebot
codebot

Reputation: 2656

ERROR: ORA-00923: FROM keyword not found where expected

I tried to fetch data from a oracle sql table with the count of records. I tried like following,

SELECT  *,
        (COUNT(BRAND_ID) AS TOTAL) 
FROM    
(
    SELECT  BRAND_ID, 
            BRAND_CODE,      
            BRAND_TITLE 
    FROM    BRAND 
    WHERE   ACTIVE = '1' 
    ORDER BY BRAND_TITLE ASC 
    OFFSET 10 ROWS 
    FETCH NEXT 10 ROWS ONLY 
) BRAND 
LEFT JOIN 
((
    SELECT  PRODUCT_ID, 
            PRODUCT_SKU_ID, 
            PRODUCT_WEB_ID, 
            PRODUCT_TITLE, 
            PRODUCT_SALES_PRICE, 
            PRODUCT_REGULAR_PRICE, 
            PRODUCT_RATING 
    FROM    PRODUCT 
    WHERE   
    (
        PRODUCT_TYPE='B' 
    OR  PRODUCT_TYPE='R' 
    ) 
    AND AVAILABILITY='1' 
)  PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE

When I'm executing this I got the following error,

ERROR: ORA-00923: FROM keyword not found where expected

How may I fix this.

Thanks in Advance!

Upvotes: 1

Views: 4318

Answers (3)

Boneist
Boneist

Reputation: 23588

I don't have 12c, so can't test, but maybe this is what you're after?

SELECT  *
FROM    
(
    SELECT  BRAND_ID, 
            BRAND_CODE,      
            BRAND_TITLE 
    FROM    (select b.*,
                    count(brand_id) over () total
             from   BRAND b
    WHERE   ACTIVE = '1' 
    ORDER BY BRAND_TITLE ASC 
    OFFSET 10 ROWS 
    FETCH NEXT 10 ROWS ONLY 
) BRAND 
LEFT JOIN 
((
    SELECT  PRODUCT_ID, 
            PRODUCT_SKU_ID, 
            PRODUCT_WEB_ID, 
            PRODUCT_TITLE, 
            PRODUCT_SALES_PRICE, 
            PRODUCT_REGULAR_PRICE, 
            PRODUCT_RATING 
    FROM    PRODUCT 
    WHERE   
    (
        PRODUCT_TYPE='B' 
    OR  PRODUCT_TYPE='R' 
    ) 
    AND AVAILABILITY='1' 
)  PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE;

This uses an analytic query to get the count of all brand_ids over the whole table before you filter the rows. I'm not sure if you wanted the count per brand_id (count(*) over (partititon by brand_id) or perhaps the count of distinct brand_ids (count(distinct brand_id) over ()), though, so you'll have to play around with the count function to get the results you're after.

Upvotes: 0

Sachu
Sachu

Reputation: 7766

You are using a aggreagte function in the select statement . So you cannot simply call Select * for other columns.

  1. First you should give an alias for the inside columns selected for easiness.
  2. Then select that columns in the outside SELECT
  3. Since one of the column in select is using agg function then a Group By should be done by other columns coming in Select.

Here for easiness i gave column name as c2,c3....rename as like u want. If no alias is given u can specify the column as it is specified.

 SELECT c2,c3,c4,c5,c6,c7,c8,c9,c10,
            COUNT(BRAND_ID) AS TOTAL 
    FROM    
    (
        SELECT  BRAND_ID ,
                BRAND_CODE AS c2,      
                BRAND_TITLE AS c3
        FROM    BRAND 
        WHERE   ACTIVE = '1' 
        ORDER BY BRAND_TITLE ASC 
        OFFSET 10 ROWS 
        FETCH NEXT 10 ROWS ONLY 
    ) BRAND 
    LEFT JOIN 
    ((
        SELECT  PRODUCT_ID AS c4, 
                PRODUCT_SKU_ID AS c5, 
                PRODUCT_WEB_ID AS c6, 
                PRODUCT_TITLE AS c7, 
                PRODUCT_SALES_PRICE AS c8, 
                PRODUCT_REGULAR_PRICE AS c9, 
                PRODUCT_RATING  AS c10
        FROM    PRODUCT 
        WHERE   
        (
            PRODUCT_TYPE='B' 
        OR  PRODUCT_TYPE='R' 
        ) 
        AND AVAILABILITY='1' 
    )  PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE
    Group By c2,c3,c4,c5,c6,c7,c8,c9,c10 

Upvotes: 0

Arun Palanisamy
Arun Palanisamy

Reputation: 5469

I guess You should remove * from select statement in the first line. Try the below one.

SELECT  (COUNT(BRAND_ID) AS TOTAL) 
FROM    
(
    SELECT  BRAND_ID, 
            BRAND_CODE,      
            BRAND_TITLE 
    FROM    BRAND 
    WHERE   ACTIVE = '1' 
    ORDER BY BRAND_TITLE ASC 
    OFFSET 10 ROWS 
    FETCH NEXT 10 ROWS ONLY 
) BRAND 
LEFT JOIN 
((
    SELECT  PRODUCT_ID, 
            PRODUCT_SKU_ID, 
            PRODUCT_WEB_ID, 
            PRODUCT_TITLE, 
            PRODUCT_SALES_PRICE, 
            PRODUCT_REGULAR_PRICE, 
            PRODUCT_RATING 
    FROM    PRODUCT 
    WHERE   
    (
        PRODUCT_TYPE='B' 
    OR  PRODUCT_TYPE='R' 
    ) 
    AND AVAILABILITY='1' 
)  PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE

Upvotes: 3

Related Questions