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. (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 ?
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.
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.