從你的詳細說明,我想你需要的是LEFT JOIN的transaksi_kas_detail和transaksi_kas表(和KAS表使用時)。
請注意,您的示例數據具有進一步細分的細節,並且不確定您是如何獲取額外數據的。
但是像這樣
SELECTid_akun, kode_akun, nama_akun, SUM(saldo_awal) as 'SaldoAwal', SUM(debet) as 'Debet', SUM(kredit) as 'Kredit', (SUM(saldo_awal) + SUM(debet) - SUM(kredit)) as 'SaldoAkhir'
from
(
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) as saldo_awal, b.amount as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2')
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2')
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4')
LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4')
LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, b.amount as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6')
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', b.amount as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6')
) fff
WHERE group_account_id = '15'
GROUP BY id_akun, kode_akun, nama_akun
ORDER BY kode_akun asc
編輯 - 檢查的日期在ON條款: -
SELECT id_akun, kode_akun, nama_akun, SUM(saldo_awal) as 'SaldoAwal', SUM(debet) as 'Debet', SUM(kredit) as 'Kredit', (SUM(saldo_awal) + SUM(debet) - SUM(kredit)) as 'SaldoAkhir'
from
(
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) as saldo_awal, IF(c.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(c.id IS NULL, 0, b.amount) as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('1', '2') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, IF(d.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', c.trx_date, (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(d.id IS NULL, 0, b.amount) as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('3', '4') AND c.trx_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
LEFT OUTER JOIN kas d ON c.kas_id = d.id AND d.jenis != '3'
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, IF(c.id IS NULL, 0, b.amount) as 'debet', 0 as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'D' AND b.cek_due_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6')
UNION ALL
SELECT a.group_account_id, a.id as 'id_akun', a.code as 'kode_akun', a.name as 'nama_akun', b.cek_due_date as 'trx_date', (saldo_awal_debet - saldo_awal_kredit) saldo_awal, 0 as 'debet', IF(c.id IS NULL, 0, b.amount) as 'kredit'
FROM account a
LEFT OUTER JOIN transaksi_kas_detail b ON a.id = b.account_id AND b.dt = 'K' AND b.cek_due_date BETWEEN '2016-01-01 00:00:00' AND '2016-11-16 00:00:00'
LEFT OUTER JOIN transaksi_kas c ON b.transaksi_kas_id = c.id AND c.trx_type IN ('5', '6')
) fff
WHERE group_account_id = '15'
GROUP BY id_akun, kode_akun, nama_akun
ORDER BY kode_akun asc
你能做出[SQLFiddle(http://sqlfiddle.com/)? –
UNION將重複複製。這是問題嗎?如果是這樣切換到聯盟所有 – Kickstart
@PraveenKumar我試過,但我的表到很多,我試圖導出從我的數據庫,然後將查詢複製到sqlFiddle,但我不知道爲什麼架構不創建 – akiong