简体   繁体   English

SQL子查询缺少表达式

[英]SQL missing expression on subquery

SELECT IME_ODDAJE
from ODDAJA
inner join (
  SELECT FK_ODDAJA2, COUNT(*) AS ST_GOSTOV
  FROM ODDAJA_GOST
  HAVING ST_GOSTOV > AVG(SELECT FK_ODDAJA2, COUNT(*) AS ST_GOSTOV FROM ODDAJA_GOST GROUP BY FK_ODDAJA2) t ON t.fk_oddaja2 = oddaja.ID_ODDAJA 
GROUP BY FK_ODDAJA2
);

**AVG(SELECT** FK_ODDAJA2, COUNT(*) AS ST_GOSTOV FROM ODDAJA_GOST GROUP BY FK_ODDAJA2)

In this bold section oracle throw me the exception that there is the missing expression and I will be glad if someone can help me with that out. 在这个大胆的部分中,甲骨文给我抛出了一个例外,那就是缺少表达,如果有人可以帮助我,我将很高兴。

here I need to count guests in the show and I need to pick out shows with a higher number of guests than Avg guests on the show. 在这里,我需要统计节目中的来宾人数,并且需要挑选出比节目中的平均来宾人数更多的来宾。

The correct syntax is to apply the aggregate function to a selected column instead of the whole query: 正确的语法是将聚合函数应用于选定的列,而不是整个查询:

   Select avg(fk_oddaja2)
        , count(*)         As st_gostov
     From oddaja_gost
 Group by fk_oddaja2
        ;

I recommend a process of iterative query development. 我建议一个迭代查询开发过程。 Start with a query that gets part of the result we need, and test that, and verify it returns the result we expect. 从获得所需结果一部分的查询开始,进行测试,并验证它是否返回我们期望的结果。 And then add the next part of the query, and test that. 然后添加查询的下一部分,并进行测试。 When we run into trouble, backup to what we had working. 当我们遇到麻烦时,请备份到我们曾经工作过的地方。

Start with a query that gets the count of guests per show: 从一个查询开始,该查询获取每个节目的来宾人数:

 SELECT g.fk_oddaja2
      , COUNT(*) AS st_gostov
   FROM oddaja_gost g
  GROUP BY g.fk_oddaja2

Next, we write a query that gets an average of those count of guests per show 接下来,我们编写一个查询,获取每个节目的平均来宾人数

 SELECT AVG(c.st_gostov) AS avg_st_gostov 
   FROM (
         SELECT g.fk_oddaja2
              , COUNT(*) AS st_gostov
           FROM oddaja_gost g
          GROUP BY g.fk_oddaja2
        ) c

As an alternative, we could get the overall average, dividing the total number of guests by the number of shows: 或者,我们可以通过将客人总数除以表演次数来获得总体平均值:

 SELECT COUNT(t.fk_oddaja2) / COUNT(DISTINCT t.fk_oddaja2) AS avg_guests 
   FROM oddaja_gost t

Next, we can combine the two queries (to return the counts that are greater than the average) using the queries above as inline views, in a pattern like this: 接下来,我们可以将上述查询用作内联视图,以类似的模式将两个查询组合起来(以返回大于平均值的计数):

SELECT ...
  FROM ( SELECT ... 
       ) a
  JOIN ( SELECT ... 
       ) c
    ON c.st_gostov > a.avg_st_gostov

Or, we could combine them using Common Table Expressions: 或者,我们可以使用通用表表达式将它们组合起来:

 WITH c AS ( SELECT g.fk_oddaja2
                  , COUNT(*) AS st_gostov
               FROM oddaja_gost g
              GROUP BY g.fk_oddaja2
           )
      a AS ( SELECT AVG(ac.st_gostov) AS avg_st_gostov
               FROM c ac
           )  
 SELECT a.avg_st_gostov
      , c.st_gostov
      , c.fk_oddaja2
   FROM c
   JOIN a
     ON c.st_gostov > a.avg_st_gostov
  ORDER BY c.st_gostov DESC

Next, we can think about adding the join to the ODDAJA table. 接下来,我们可以考虑将联接添加到ODDAJA表中。 (Seems like the original query is missing a condition to "match" rows from ODDAJA.) We can venture a guess as to what that condition should be... maybe FK_ODDAJA2 from ODDAJA_GOST should match ODDAJA2 from ODDAJA ? (似乎原始查询缺少要“匹配”来自ODDAJA的行的条件。)我们可以冒险猜测该条件应该是...也许ODDAJA_GOST的FK_ODDAJA2应该与ODDAJA的ODDAJA2相匹配?

 WITH c AS ( SELECT g.fk_oddaja2
                  , COUNT(*) AS st_gostov
               FROM oddaja_gost g
              GROUP BY g.fk_oddaja2
           )
      a AS ( SELECT AVG(ac.st_gostov) AS avg_st_gostov
               FROM c ac
           )  
 SELECT o.ime_oddaje
      , a.avg_st_gostov
      , c.st_gostov
      , c.fk_oddaja2 
   FROM c
   JOIN a
     ON c.st_gostov > a.avg_st_gostov
   JOIN oddaja o
     ON o.oddaja2 = c.fk_oddaja2
  ORDER BY c.st_gostov DESC 

That's the approach I would take: building the query iteratively, step by step, with each step building on the previous step. 这就是我要采取的方法:逐步地逐步构建查询,而每一步都基于上一步。

It's helpful to have some example data and expected output , so we can test each step, and have something to compare our results to. 拥有一些示例数据预期输出会很有帮助,因此我们可以测试每个步骤,并可以将结果进行比较。


Or, we can just try to fix the syntax in the orignal query. 或者,我们可以尝试修复原始查询中的语法。 This might resolve the syntax error. 这可能会解决语法错误。 (This also might not return a result that satisfies the specification.) (这也可能不会返回满足规范的结果。)

SELECT IME_ODDAJE from ODDAJA inner join (SELECT FK_ODDAJA2, COUNT(*) AS ST_GOSTOV FROM ODDAJA_GOST GROUP BY FK_ODDAJA2 HAVING ST_GOSTOV > (SELECT AVG(ST_GOSTOV) FROM (SELECT FK_ODDAJA2, COUNT(*) AS ST_GOSTOV FROM ODDAJA_GOST GROUP BY FK_ODDAJA2))) ON FK_ODAJA2 = ODDAJA2 

Whichever. 任何。

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

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