0

That is the required output. click for image

My query is- http://sqlfiddle.com/#!17/9e89e/761

On average profit, the formula needs to group by aggregate function.So not getting required profit My query is attached above link and the required output is attached by image. please help me.

4
  • Please do not add random irrelevant product tags to your question! Commented Aug 12, 2021 at 6:56
  • I thought it will reach multiple developers. Next time I will take care Commented Aug 12, 2021 at 7:00
  • 1
    Please do not upload images of code/errors when asking a question. Commented Aug 12, 2021 at 7:04
  • 2
    @anupamtiwari yes, your question reached more developers and then annoyed them as it had nothing to do with the product they know... Commented Aug 12, 2021 at 7:28

1 Answer 1

1

You can get the count of the different universe first using count(*) over (partition by u.universe) and after that you can apply your logic to calculate profit like the following query.

select c.id,c.name,u.universe,u.budget,u.revenue
    ,(revenue - budget) / count(*) over (partition by u.universe) as profit
from universe u
inner join characters c on u.id = c.universe_id
order by u.universe desc

Check the updated fiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need a subquery for this. You can divide by count(*) over (...) directly.
@ThorstenKettner, you are right, thanks for the suggestion, I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.