0
votes

There is six games and I want to write query to output the net sales amount for each games and total sales based on Top 10 Retailers. Also, I will be great if you can output the percent of total sales for each retailer. Here is MySQL query:

   SELECT Concat(css.retailerid,'-',rmd.retailer)            AS retailer,
          rmd.city,
          CASE
                 WHEN css.gameid= 1 THEN Sum(css.netsales)
          END                                                AS 'Meqa 5/36',
          CASE
                 WHEN css.gameid= 2 THEN Sum(css.netsales)
          END                                                AS '6/40',
          CASE
                 WHEN css.gameid= 3 THEN Sum(css.netsales)
          END                                                AS '4+4',
          CASE
                 WHEN css.gameid= 6 THEN Sum(css.netsales)
          END                                                AS 'Meqa 5',
          CASE
                 WHEN css.gameid= 9 THEN Sum(css.netsales)
          END                                                AS 'Super Keno',
          Sum(css.netsales)                                  AS netsalesamount,
          -- sum(css.NetSales)*100/(select sum(ss.NetSales) from reporting.core_sales_statistics AS ss) AS PERCENT,
          -- sum(css.netsalesqty) AS netsalesqty,
          -- Sum(css.NetSalesQty)*100/(select sum(ss.NetSalesQty) from reporting.core_sales_statistics AS ss) AS PERCENT
     FROM reporting.core_sales_statistics AS css 
LEFT JOIN reporting.retailer_master_data AS rmd ON css.retailerid=rmd.retailerid
    WHERE css.datastatus=1
      AND rmd.reportinglevel  IN ('Exclusive Agents', 'Distributors' )
      AND rmd.reportinglevel3 IN ('Exclusive Agents', 'Distributor Agents')
      AND css.sourcesystem ='AEGIS'
      AND css.transactiondate = curdate() -1
      AND css.gamecategoryid=4
 GROUP BY css.retailerid,
          rmd.retailer
 ORDER BY netsalesamount DESC limit 10
        ;
Make it easy to assist you - simplify! minimal reproducible examplejarlh
use aggregration functions aroud the case When try first MAXnbk
How to ask says: "Search, and research", so you should have tried: stackoverflow.com/search?q=%5Bmysql%5D+sum+case+whenLuuk
Your LEFT JOIN returns regular INNER JOIN result. Move the rmd conditions from WHERE to ON to get true LEFT JOIN result.jarlh