2016-11-16 83 views
0

私のクエリから完全なレコードを取得するのは本当に混乱します。クエリからすべてのデータを取得select unionで選択mysql

私はすでにベストを試していますが、私はまだ私が望むものを得ることができません。

私はmysql 5.5を使用していますが、私は実際に何をやり直すべきか分かりません。

私のクエリが5つ以上のテーブルを結合するため、私はdatabase.sqlを共有します。

database_schema.sql

、これは私が前に試した私のクエリです。

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, b.amount as 'debet', 0 as 'kredit' 
from account a, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='D' AND c.trx_type IN ('1', '2') 
UNION 
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, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='K' AND c.trx_type IN ('1', '2') 
UNION 
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, transaksi_kas_detail b, transaksi_kas c, kas d 
where a.id=b.account_id AND b.transaksi_kas_id=c.id AND c.kas_id=d.id 
    AND dt='D' AND c.trx_type IN ('3', '4') AND d.jenis != '3' 
UNION 
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, transaksi_kas_detail b, transaksi_kas c, kas d 
where a.id=b.account_id AND b.transaksi_kas_id=c.id AND c.kas_id=d.id 
    AND dt='K' AND c.trx_type IN ('3', '4') AND d.jenis != '3' 
UNION 
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, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='D' AND c.trx_type IN ('5', '6') 
UNION 
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, transaksi_kas_detail b, transaksi_kas c 
where a.id=b.account_id AND b.transaksi_kas_id=c.id 
    AND dt='K' 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 

私はこのクエリを実行した後..

私はこの結果を取得します。

enter image description here

私はちょうど2レコードを取得、私は、テーブルのアカウントが10行を持っている場合は、その後、私のクエリは10行を表示しなければならないテーブルからaccount

を取得ORDER BY kode_akunことにより、すべてのデータを取得する必要があります。

多分誰かがこのような例のために私に

を助けることができます。..

enter image description here

+0

あなたは[SQLFiddle](http://sqlfiddle.com/)を作ることができますか? –

+1

UNIONは重複しています。それはここの問題ですか?もしUNION ALL – Kickstart

+0

に切り替える@PraveenKumar私のテーブルを大いに試しましたが、データベースからエクスポートしようとしましたが、クエリをsqlFiddleにコピーしようとしましたが、なぜスキーマが作成されないのかわかりません。 – akiong

答えて

1

あなたから私は何が必要だと思うさらに説明が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 
+0

あなたのコードはすでにうまく実行されますが、 'WHERE group_account_id = 15とtrx_date> = '2016-01-01 00:00:00'とtrx_date <= '2016-11-16 00:00:00'を追加すると結果になります。 – akiong

+0

@akiong - trx_dateの値が来ますLEFT OUTER JOINされたテーブルから、一致するレコードがない場合はNULLになります。外部クエリのWHERE句でこれらの値をチェックすると、これらのレコードは除外されます。_transaksi_kas_detail_の各LEFT OUTER JOINのON句にチェックを入れてください。これは、SUMから除外しますが、アカウントの詳細を保持することでした。 – Kickstart

+0

ああ、レコードがtrx_dateと一致しないので、あなたのクエリのようにレコードがtrx_dateの 'NULL'でも取得できます。レコードが' transaksi_kas_detail'でなくても、レコードが表示されます – akiong

関連する問題