Assistance with Group By with Rollup

79 Views Asked by At

I have a query that from an existing problem I have been working on. I would like to include authors who have 'no books' and books with 'no authors' (use '_anon' as the author id in that case). Below is a working problem but I need '_anon' under Author_ID where there are no authors along with 'no books' under Book_ID where there are no books. I think I have figured out 'no sales':

SELECT COALESCE(author_id, 'All Authors') author_id
     , COALESCE(book_id, IF(author_id IS NULL, 'All Books', 'All Books')) book_id
     , NumOrders  
     , COALESCE(TotalSales, 'No Sales') TotalSales   
 FROM 
(    
 SELECT author_id 
      , b.book_id 
      , COALESCE(SUM(order_id), 0) NumOrders  
      , SUM(quantity * order_price) TotalSales   
   FROM a_bkinfo.book_authors b 
   LEFT JOIN a_bkorders. order_details d 
     ON b.book_id = d.book_id
  WHERE author_sequence = 1           
  GROUP BY Author_id, Book_ID WITH ROLLUP  
) q;
0

There are 0 best solutions below