i have 2 tables and I'm trying to build a query but i don't have the correct results, can someone help me please ?
Table 1 :
code|name
74|Jhon
06|Sara
92|Fred
75|Fred
06|Sara
13|Lola
Table 2 :
code|amount|folder
06|500|1
74|135|0
13|150|0
13|100|1
92|200|1
75|250|0
06|300|1
I want to select, by name, the amount and total number of folders and number of folders with 0.
I did the following query :
select table1.name,
sum(table2.amount) as amount_tot,
count(table2.folder) as nb_folder,
sum(table2.folder) as nb_folder_ko
from table1 inner join table2 on table1.code=table2.code
group by name
I have the following results :
name|amount_tot|nb_folder|nb_folder_ko
Lola|250|2|1
Fred|450|2|1
Sara|1600|4|4
Jhon|135|1|0
As you can see, it's not correct regarding my source tables, i think my query do some double count but i don't know how to fix it, please help me :)