简体   繁体   中英

Alias not working in inner join

I'm trying to give an alias to the table facturation and to the table member in the inner join so I can use it in the sub query in the select like this

    SELECT description,
       Avg((SELECT Count(*)
            FROM   facturation F
            WHERE  F.membreid = M.membreid))
FROM   v_type_membre
       INNER JOIN v_membre M
               ON v_type_membre.typeid = M.typeid
       INNER JOIN v_facturation
               ON M.membreid = v_facturation.membreid
GROUP  BY description
ORDER  BY description; 

Why do I get the error :

Error at Command Line:3 Column:31
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:

Thank you

Your original problem was the quotes around the aliases. You don't need them.

Similarly, you don't need the correlated subquery. You can write the query as:

select description, avg(cnt*1.0) as avg_cnt
FROM v_type_membre INNER JOIN v_membre M
     ON v_type_membre.typeid = M.typeid INNER JOIN
     v_facturation
     ON M.membreid = v_facturation.membreid inner join
     (SELECT f.membreid, Count(*) as cnt
      FROM facturation F
      group by F.membreid
     ) cnt
     on cnt.membreid = M.membreid
GROUP  BY description
ORDER  BY description;  

In addition to being clearer -- to some eyes -- this also lets you include the minimum and maximum values, or to count the number of times the value is 10 and 42, if you wanted.

I also added a "*1.0" to convert the count to a floating point number. In some databases, the average of an integer is an integer, and that is probably not what you want. Oracle does the average correctly, but I'm in the habit of doing this.

try moving the subselect to a join (I don't think you'll need the v_facturation join, I would need more info (I'm guessing v_* is a view) to be sure.);

SELECT description, Avg(F.fact_cnt)
FROM   v_type_membre
       INNER JOIN v_membre AS M
               ON v_type_membre.typeid = M.typeid
--       INNER JOIN v_facturation
--               ON M.membreid = v_facturation.membreid
       INNER JOIN (SELECT membreid, Count(*) 'fact_cnt'
            FROM   facturation group by membreid) AS F
            ON  F.membreid = M.membreid
GROUP  BY description
ORDER  BY description;

(sorry, no Oracle nor any other DB currently available, this is all parsed and compiled in my head...)

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