简体   繁体   中英

How to use group by with left and inner join in a same query?

I am using a query with inner join and left join to get data from 3 tables. I also want to use group by using matname. Because there will be multiple outputs from same matname. I want to get the sum of them.

Table tbl_sales

+------------+--------------+---------------+----------+--------+---------+
| company    | customer     | date          | material | matgrp | qty_ach |
+------------+--------------+---------------+----------+--------+---------+
| 2100       | 30000002     | 2017-01-10    |  2000012 | FG0001 |    2    |
| 2200       | 30000002     | 2017-01-10    |  2000012 | FG0001 |    4    |
| 2100       | 30000003     | 2017-01-10    |  2000011 | FG0001 |    5    |
| 2100       | 30000002     | 2017-01-11    |  2000024 | FG0001 |    4    |
| 2300       | 30000004     | 2017-01-11    |  2000010 | FG0001 |    1    |
+------------+--------------+---------------+----------+--------+---------+

Table tbl_mas_material

+---------+--------------+--------+
| matnum  | matname      | matgrp | 
+---------+--------------+--------+
| 2000011 | name1        | FG0001 | 
| 2000012 | name2        | FG0001 |  
| 2000013 | name3        | FG0001 |    
| 2000014 | name4        | FG0001 |   
| 2000015 | name5        | FG0001 |    
+---------+--------------+--------+

Table tbl_mas_customer

+---------+----------+--------+
| customer| rep      | namez  | 
+---------+----------+--------+
| 2000011 | 501      | ddd    | 
| 2000012 | 502      | bbb    |  
| 2000013 | 501      | zzz    |    
| 2000014 | 501      | qqq    |   
| 2000015 | 503      | ttt    |    
+---------+----------+--------+

I want to get the quantity(qty_ach) of certain material group(matgrp) with its name(matname) that specific rep has sold.

This is the query i use. This gives me the correct output.

SELECT tbl_mas_material.matname, tbl_sales.qty_ach 
FROM salesforce.tbl_sales 
inner join salesforce.tbl_mas_customer on salesforce.tbl_mas_customer.customer = salesforce.tbl_sales.customer 
left join salesforce.tbl_mas_material on salesforce.tbl_mas_material.matnum = salesforce.tbl_sales.material 
where salesforce.tbl_sales.matgrp = 'FG0001' 
and DATE_FORMAT(date, '%Y-%m') = '2017-01' 
and salesforce.tbl_mas_customer.rep = '501'

But i want to group by them using matname because i want to get the sum of qty_ach using material under same name. But if i add group by matname at last it wont give me the correct output with the sum of the qty_ach

without group by

    {"feed":[
    {"matname":"3.00-10 4PR SECURA NEO TL CEAT","qty_ach":"5"},
    {"matname":"3.50-10 04PR SECURA NEO CEAT","qty_ach":"2"},
    {"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"1"},
    {"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"17"},
    {"matname":"3.00-18 06PR SECURA SPORT CEAT (R)","qty_ach":"3"},
{"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
    {"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"6"},
    {"matname":"2.75-18 04PR SECURA ZOOM (F)","qty_ach":"-11"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"-11"},       
{"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"11"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
    {"matname":null,"qty_ach":"22"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"11"},
    {"matname":"2.50-17 04PR SECURA SPORT CEAT  (R)","qty_ach":"4"},
    {"matname":"2.50-17 04PR SECURA ZOOM FX (F)","qty_ach":"3"},
    {"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"2"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"22"},
    {"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"6"}
{"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"22"},
    {"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"5"},
    {"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"6"},
    {"matname":"100\/90-18 6PR SECURA ZOOM TL","qty_ach":"12"},
    {"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"4"},
    {"matname":"2.75-14 06PR SECURA 275","qty_ach":"2"},
    {"matname":"2.75-14 06PR SECURA 275","qty_ach":"6"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"10"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"3"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"27"},
    {"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"27"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
    {"matname":"3.00-17 04PR GRIPP","qty_ach":"11"},
    {"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
    {"matname":"120\/80-17 SECURA PULSE","qty_ach":"6"},
    {"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"5"},
{"matname":null,"qty_ach":"22"},
    {"matname":null,"qty_ach":"38"},
    {"matname":null,"qty_ach":"11"},
    {"matname":null,"qty_ach":"11"},
    {"matname":null,"qty_ach":"22"},
    {"matname":null,"qty_ach":"5"},
    {"matname":null,"qty_ach":"-5"},
    {"matname":null,"qty_ach":"7"}]}

With group by tbl_mas_material.matname

{"feed":[{"matname":null,"qty_ach":"22"},
{"matname":"100\/90-17 GRIPP TL CEAT","qty_ach":"6"},
{"matname":"100\/90-17 SECURA ZOOM CEAT TL","qty_ach":"22"},
{"matname":"100\/90-18 6PR SECURA ZOOM TL","qty_ach":"12"},
{"matname":"100\/90-18 GRIPP TL CEAT","qty_ach":"5"},
{"matname":"120\/80-17 SECURA PULSE","qty_ach":"6"},
{"matname":"2.50-17 04PR SECURA SPORT CEAT  (R)","qty_ach":"4"},{"matname":"2.50-17 04PR SECURA ZOOM FX (F)","qty_ach":"3"},
{"matname":"2.75-14 06PR SECURA 275","qty_ach":"2"},
{"matname":"2.75-17 04PR SECURA ZOOM CEAT (F)","qty_ach":"17"},
{"matname":"2.75-17 04PR SECURA ZOOM TL","qty_ach":"10"},
{"matname":"2.75-18 04PR SECURA ZOOM (F)","qty_ach":"-11"},
{"matname":"3.00-10 4PR SECURA NEO TL CEAT","qty_ach":"5"},
{"matname":"3.00-17 04PR GRIPP","qty_ach":"33"},
{"matname":"3.00-17 06PR SECURA ZOOM CEAT (R)","qty_ach":"-11"},{"matname":"3.00-18 06PR SECURA SPORT CEAT (R)","qty_ach":"3"},
{"matname":"3.00-18 6PR SECURA ZOOM TL","qty_ach":"1"},
{"matname":"3.50-10 04PR SECURA NEO CEAT","qty_ach":"2"}]}

The problem is that is not the output what I want. See for example matname:null . it only gave me 22 after use group by. But I want sum of them (matname as null) : 22+38+11+11+22+5-5+7 = 111

Well, I'm making a guess here because you have not shared the exact expected output. However, based on the fact you wrote you wanted "to get the sum of qty_ach" and in the current output qty_ach is not summed up, you are not using the sum() group by function in your query. Without that MySQL cannot tell that you wanted to sum this field.

The query in this case would look like something along the below lines:

SELECT tbl_mas_material.matname, sum(tbl_sales.qty_ach) as  qty_ach_sum
FROM salesforce.tbl_sales 
inner join salesforce.tbl_mas_customer on salesforce.tbl_mas_customer.customer = salesforce.tbl_sales.customer 
left join salesforce.tbl_mas_material on salesforce.tbl_mas_material.matnum = salesforce.tbl_sales.material 
where salesforce.tbl_sales.matgrp = 'FG0001' 
and DATE_FORMAT(date, '%Y-%m') = '2017-01' 
and salesforce.tbl_mas_customer.rep = '501'
group by tbl_mas_material.matname

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