How can I correct this query that involves a CASE statement for a summary?

Theodocius

I'm currently trying to solve an issue revolving around summarizing a list of publishers, their total revenue, total payouts, and their net profit. What makes this slightly complicated is that the total payout is contingent on a case statement (due to having to choose between the higher value of royalties). This case statement was perfectly fine and executed in a previous query that you can see on the SQLFiddle link down below. My issue is that I have a near finished query that addresses what I need but I don't know what correction to make for it to complete. Help would be super appreciated! And if you get it to work, you would be a legit lifesaver!!

 Select name,
SUM(book.msrp) AS 'Total Revenue',
SUM(EarningByBook) AS 'Total Payouts',
SUM(book.msrp)-SUM(EarningByBook) AS 'Net Profit'
FROM
(SELECT publisher.name, book.msrp,
(SELECT 
       CASE WHEN preferred_royalties > standard_royalties
                THEN preferred_royalties*copies_sold
                ELSE standard_royalties*copies_sold END 
       AS 'EarningByBook', 
       copies_sold , 
       YEAR(CURDATE())-YEAR(date_published) Years
INNER JOIN book ON publisher.id = book.publisher_id)
FROM author A 
JOIN book B ON A.id=B.author_id 
JOIN publisher P ON B.publisher_id=P.id)
From publisher
INNER JOIN book ON publisher.id = book.publisher_id) Z
GROUP BY 
 name;

The SQL fiddle is as follows : https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b0015a0a4286f9b2c064bbd65525faa5&hide=13312

The output expected should look

Publisher Total Revenue Total Payouts Net Profit
name 20000 1500 18500
name 15000 1000 14000
Parfait

Consider flattening all your inner selects to a single SELECT subquery.

SELECT sub.publisher
     , SUM(sub.msrp) AS 'Total Revenue'
     , SUM(sub.EarningByBook) AS 'Total Payouts'
     , SUM(sub.msrp) - SUM(sub.EarningByBook) AS 'Net Profit'
FROM
  (SELECT 
        P.`name` AS publisher
      , CASE 
          WHEN A.preferred_royalties > P.standard_royalties
          THEN A.preferred_royalties * B.copies_sold
          ELSE P.standard_royalties * B.copies_sold 
        END AS EarningByBook
     , YEAR(CURDATE()) - YEAR(B.date_published) AS Years
     , B.msrp
     , B.copies_sold
     
  FROM author A 
  INNER JOIN book B 
     ON A.id = B.author_id 
  INNER JOIN publisher P 
     ON B.publisher_id = P.id
  ) AS sub
  
GROUP BY 
  sub.publisher;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i loop through These with A CASE statement?

From Dev

How would I do this query with a case statement in SQLAlchemy?

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I sort on the results of a case statement in Spring Data?

From Dev

how can i use case statement after using left join?

From Dev

How can I make my switch statement case insensitive?

From Dev

How can I write this ElseIf as a Case statement in Excel-VBA?

From Dev

How can I use a case statement with a Drop Box? Access Database

From Dev

how can i use case statement after using left join?

From Dev

How can I use conditional constructs in a bash case statement?

From Dev

How can I put the below java query in switch case?

From Dev

How can i use case alias in a select query

From Dev

How Can i write update statement in mysql with select query?

From Dev

How can I fit this UPDATE query into this existing SELECT statement?

From Dev

How can i add two WHERE statement in MySQL Query?

From Dev

How to use Not Equal with CASE statement in SQL query

From Dev

Can I use an array in a case statement?

From Dev

(SQL) In a query, how do I use a CASE statement that would only conditionally require a JOIN?

From Dev

How can I structure a mocha unit test that involves promises and third party NPM modules?

From Dev

How can I use variables in an update operation in mongodb that involves an array or another type of embedded or nested document?

From Dev

Can i use user input statement inside switch case statement?

From Dev

How can I correct my ORM statement to show all friends not associated with a user in Django?

From Dev

Case Statement In Update Query

From Dev

Case statement query

From Dev

mysql query with case statement

From Dev

Case statement in a SQL query

From Dev

VBScript Case statement query

From Dev

Case statement query

Related Related

  1. 1

    How can i loop through These with A CASE statement?

  2. 2

    How would I do this query with a case statement in SQLAlchemy?

  3. 3

    How can I ensure that correct function is called in case there are multiple candidates

  4. 4

    How can I ensure that correct function is called in case there are multiple candidates

  5. 5

    How can I sort on the results of a case statement in Spring Data?

  6. 6

    how can i use case statement after using left join?

  7. 7

    How can I make my switch statement case insensitive?

  8. 8

    How can I write this ElseIf as a Case statement in Excel-VBA?

  9. 9

    How can I use a case statement with a Drop Box? Access Database

  10. 10

    how can i use case statement after using left join?

  11. 11

    How can I use conditional constructs in a bash case statement?

  12. 12

    How can I put the below java query in switch case?

  13. 13

    How can i use case alias in a select query

  14. 14

    How Can i write update statement in mysql with select query?

  15. 15

    How can I fit this UPDATE query into this existing SELECT statement?

  16. 16

    How can i add two WHERE statement in MySQL Query?

  17. 17

    How to use Not Equal with CASE statement in SQL query

  18. 18

    Can I use an array in a case statement?

  19. 19

    (SQL) In a query, how do I use a CASE statement that would only conditionally require a JOIN?

  20. 20

    How can I structure a mocha unit test that involves promises and third party NPM modules?

  21. 21

    How can I use variables in an update operation in mongodb that involves an array or another type of embedded or nested document?

  22. 22

    Can i use user input statement inside switch case statement?

  23. 23

    How can I correct my ORM statement to show all friends not associated with a user in Django?

  24. 24

    Case Statement In Update Query

  25. 25

    Case statement query

  26. 26

    mysql query with case statement

  27. 27

    Case statement in a SQL query

  28. 28

    VBScript Case statement query

  29. 29

    Case statement query

HotTag

Archive