あなたから私は何が必要だと思うさらに説明がtransaksi_kas_detailとtransaksi_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
EDITのようなもの - 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は重複しています。それはここの問題ですか?もしUNION ALL – Kickstart
に切り替える@PraveenKumar私のテーブルを大いに試しましたが、データベースからエクスポートしようとしましたが、クエリをsqlFiddleにコピーしようとしましたが、なぜスキーマが作成されないのかわかりません。 – akiong