简体   繁体   English

别名不在内连接中工作

[英]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 我试图给表facturation和内连接中的表成员一个别名,所以我可以在select中的子查询中使用它像这样

    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. 除了更清晰 - 对某些人来说 - 这也可以让你包括最小值和最大值,或者计算值为10和42的次数,如果你愿意的话。

I also added a "*1.0" to convert the count to a floating point number. 我还添加了一个“* 1.0”来将计数转换为浮点数。 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. Oracle正确地做了平均值,但我习惯这样做。

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.); 尝试将子选择移动到连接(我不认为你需要v_facturation连接,我需要更多的信息(我猜v_ *是一个视图),以确保。);

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...) (对不起,没有Oracle或任何其他目前可用的数据库,这都是在我脑海中解析和编译的......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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