2016-08-04 12 views
-1
 
funds_table 
--------------------------- 
source_id, amount, date, 
--------------------------- 
1, 1000, 2016-01-15 
2, 2000, 2016-02-28 
 
ca_table 
--------------------------- 
ca_id,source_id, amount 
--------------------------- 
c1 ,  1, 500 
c2 ,  1, 500 
c3 ,  2, 900 
c4 ,  2, 1100 
 
exp_table 
--------------------------- 
exp_id, ca_id, amount, 
--------------------------- 
e1,  c1, 0 
e2,  c1, 250 
e3,  c2, 500 
e4,  c3, 500 
e5,  c4, 600 
e6,  c4, 500 

Ich möchte die select MONTH (Datum), sum (funds_table.amount **) summieren, Summe (ca_table.amount), sum (exp_table.amount) verbinden sich wo Jahr (Datum) = 2016 und Gruppe von Monat (Datum)Mysql Summe Spalte verschiedene 3-Tabellen

erwartete Ausgabe

 

----------------------------------------------------------------------- 
MonthName(date),sum(source_id.amount),sum(ca_id.amount),sum(exp_id.amount) 
----------------------------------------------------------------------- 
Jan   ,     1000,    1000,    750 
Feb   ,     2000,    2000,    1100 

3 Tagen Suche nach Ergebnis, aber ich kann das genaue Ergebnis.

+0

Was hav eyou versucht, und wo ist dein Problem? – Jens

+0

ich will die letzte tabelle wird ergebnis –

Antwort

0

Ich habe Ihre Tabellen nicht, aber ich denke, das U Hilfe wird

SELECT DATE_FORMAT(funds_table.date,"%M") AS month,SUM(ca_table.amount) AS ca_table_amount,SUM(exp_table.amount) AS exp_table_amount,month,SUM(funds_table.amount) AS funds_table_amount FROM `funds_table` INNER JOIN ca_table 
ON funds_table.source_id=ca_table.source_id 
INNER JOIN exp_table 
ON ca_table.ca_id=exp_table.ca_id 
GROUP BY month 
0

nicht so einfach, wie es zunächst scheint - die Vereinigung Anmerkung für die Join durch ca_table gerecht zu exp_table

/* 
create table funds_table (source_id int, amount int, dte date) ; 
insert into funds_table values 
(1, 1000, '2016-01-15'), 
(2, 2000, '2016-02-28'); 

create table ca_table (ca_id varchar(2),source_id int, amount int); 
insert into ca_table values 
('c1' ,  1, 500), 
('c2' ,  1, 500), 
('c3' ,  2, 900), 
('c4' ,  2, 1100); 

create table exp_table(exp_id varchar(2), ca_id varchar(2), amount int); 
insert into exp_table values 
('e1',  'c1', 0), 
('e2',  'c1', 250), 
('e3',  'c2', 500), 
('e4',  'c3', 500), 
('e5',  'c4', 600), 
('e6',  'c4', 500); 
*/ 

select month,sum(ftamt) ftamt,sum(caamt) caamt,sum(eamt) eamt 
from 
(
select month(ft.dte) month,sum(ft.amount) as ftamt, ca.caamt, 0 as eamt 
from  funds_table ft 
join  (select ca.source_id ,sum(ca.amount) caamt from ca_table ca group by ca.source_id) ca on ca.source_id = ft.source_id 
group  by month(ft.dte) 
union 
select month(ft.dte), 0,0, sum(e.amount) 
from  funds_table ft 
join  ca_table ca on ca.source_id = ft.source_id 
join  exp_table e on e.ca_id = ca.ca_id 
group  by month(ft.dte) 
) s 
group by month 

Ergebnis

+-------+-------+-------+------+ 
| month | ftamt | caamt | eamt | 
+-------+-------+-------+------+ 
|  1 | 1000 | 1000 | 750 | 
|  2 | 2000 | 2000 | 1600 | 
+-------+-------+-------+------+ 
+0

ich werde versuchen und ich denke, es wird dank laufen! Ich werde so schnell wie möglich antworten –