Mysql SUM inside Left Join return wrong value when using with rollup function
0
votes
0
answers
59
views
Good Afternoon.
I have some problem/missunderstood with rollup function when using LEFT Join statement. the sum from rollup function at the end is not like my expected. please see the sample table data from my table
***tb_no_penjualan***
id_no_jual | total_bayar | created_date
----------------------------------------------
2 | 1024176.00 | 2024-10-23 20:40:10
3 | 520000.00 | 2024-10-23 20:47:39
4 | 241000.00 | 2024-10-23 20:55:26
7 | 346280.00 | 2024-10-24 08:55:22
8 | 957840.00 | 2024-10-24 08:57:49
9 | 534064.00 | 2024-10-24 09:04:32
12 | 731864.00 | 2024-10-24 09:34:06
***tb_det_jual***
id_det_jual | id_no_jual | jumlah_item | total_harga
-----------------------------------------------------
2 | 2 | 2 | 300000.00
3 | 2 | 2 | 50000.00
4 | 3 | 25 | 237500.00
5 | 4 | 2 | 331500.00
6 | 7 | 1 | 150000.00
7 | 7 | 5 | 252200.00
8 | 7 | 5 | 50000.00
9 | 7 | 5 | 100000.00
10 | 7 | 2 | 329800.00
11 | 8 | 3 | 494700.00
12 | 9 | 4 | 201760.00
13 | 12 | 3 | 494700.00
14 | 12 | 5 | 50000.00
15 | 12 | 7 | 140000.00
i have try to use this query
SELECT a.id_no_jual,
date(a.created_date) as tanggal,
SUM(a.jumlah) as jumlah_item,
SUM(a.total_harga) AS total_harga,b.total_bayar
from pos.tb_det_jual a
LEFT JOIN ( SELECT id_no_jual as id_no_jual,
SUM(total_bayar) as total_bayar
FROM pos.tb_no_penjualan
group by date(created_date) ) as b ON(a.id_no_jual=b.id_no_jual)
group by date(a.created_date) WITH ROLLUP;
and the result from that query like this:
id_no_jual | tanggal | jumlah_item | total_harga | total_bayar
---------------------------------------------------------------
2 |2024-10-23 | 31 | 919000.00 | 1107280.00
7 |2024-10-24 | 40 | 2263160.00 | 2620739.20
7 |Null | 71 (correct)| 3182160.00(ok) | 2620739.20 (wrong)
the result i wanted like this:
id_no_jual | tanggal | jumlah_item | total_harga | total_bayar
-------------------------------------------------------------------
2 |2024-10-23 | 31 | 919000.00 | 1107280.00
7 |2024-10-24 | 40 | 2263160.00 | 2620739.20
7 |Null | 71 (correct)| 3182160.00(ok) | 3728019.20
is any mistake from my query? please correct the query if i wrong,
Regards.
Asked by epm.007
(29 rep)
Oct 24, 2024, 07:34 AM
Last activity: Oct 24, 2024, 11:44 AM
Last activity: Oct 24, 2024, 11:44 AM