简体   繁体   中英

MySQL nested sums with multiple joins and group by

I have following tables my result is always incorrect

Table 1 : product_category

  |  id |   title       |
  |  1  |   Electronics |
  |  2  |   Kitchen     |

Table 2 : sold_items

| id | product_id | invoiceid | product_category_id | qty |
| 1  | 91         | 1001      | 1                   |  2  |
| 2  | 92         | 1001      | 1                   |  3  |
| 3  | 93         | 1002      | 2                   |  7  |
| 4  | 94         | 1002      | 2                   |  3  |
| 5  | 93         | 1003      | 2                   |  15  |

Table 3 : profit_table

| invoiceid | profit | cost   |
| 1001      | 200.00 | 980.00 |
| 1002      | 100.00 | 700.00 |
| 1003      | 350.00 | 900.00 |

Table 4 : product_log

| id | product_id | qty | tdate       | invoiceid |
| 1  | 91         |  2  | 2017-01-10  | 1001      |
| 2  | 92         |  3  | 2017-01-10  | 1001      |
| 3  | 93         |  7  | 2017-02-10  | 1002      |
| 4  | 94         |  3  | 2017-02-10  | 1002      |
| 5  | 93         |  15 | 2017-03-10  | 1003      |

I want to sum(profit_table.profit), MONTH(product_log.tdate),SUM(sold_items.qty) Where result should be product_category wise and month wise and where condition should be like YEAR(product_log.tdate)=2017

Example result_view

| title      | MONTH(product_log.tdate) | sum(profit) |SUM(sold_items.qty) |
| Electronics| 1                        | 200         | 5                  |
| Kitchen    | 2                        | 100         | 10                 |
| Kitchen    | 3                        | 350         | 15                 |

Please suggest me a correct query

As per my research result will like

SELECT r.totalqty,
       f.profit,
       p.mymonth,
       c.title
FROM (SELECT invoiceid,Sum(qty) totalqty,product_category_id 
        FROM   sold_items 
        GROUP  BY sold_items.product_category_id) AS r 
       LEFT JOIN (SELECT invoiceid,Sum(profit) profit FROM profit_table GROUP BY profit_table.invoiceid) AS f ON r.tid = f.tid
 LEFT JOIN (SELECT tid,DATE(tdate) mymonth FROM pstatements) AS p 
        ON r.invoiceid = p.invoiceid
 LEFT JOIN (SELECT id,title FROM product_category) AS c 
        ON r.product_category_id = c.id
 WHERE YEAR(p.mymonth)='2017' GROUP BY DATE(p.tdate)

This is not a correct result still a clue if you want answer

The below written query should work.

You can access the sqlfiddle for this schema here .

select pc.title, month(pl.tdate), pt.profit, sum(si.qty) 
from product_category pc
inner join sold_items si on si.product_category_id = pc.id
inner join profit_table pt on pt.invoiceid = si.invoiceid
inner join product_log pl on pl.invoiceid = pt.invoiceid
where si.product_id = pl.product_id  
group by pc.title,pl.tdate
order by month(pl.tdate) asc ; 

YOU should use SOME JOIN AND group by

select  
    product_category.title
  , MONTH(product_log.tdate)
  , sum(profit)
  , SUM(sold_items.qty) 
from product_category 
left join  sold_items on  sold_items.product_category_id =  product_category.id
left join product_log  on product_log.product_id = sold_items.product_id
left join profit_table on profit_table.invoiceid = product_log.invoiceid
group by  product_category.title, MONTH(product_log.tdate)

You would need to join the tables on their keys and group by the product and transaction month. Something like the below:

select pc.title, month(pl.tdate) as tmonth, sum(pt.profit) as month_profit, sum(si.qty) as month_qty
  from product_category pc
  left join sold_items si
    on si.product_category_id = pc.id
  left join profit_table pt
    on pt.invoice_id = si.invoice_id
  left join product_log pl
    on pl.product_id = si.product_id
   and pl.invoice_id = si.invoice_id
 group by pc.id, month(pl.tdate)
 where year(pl.tdate) = '2017';

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM