2017-05-31 3 views
0

で左側に配置されます一番下に置くことで、私は3つのテーブルを持っていますすべて請求書をグループ化し、左側に合計デビットクレジットをグループ化し、合計をjsoとします。 課金でいくつかの顧客がdebitcreditでは、同じではありませんので、LEFT JOINはないJSOではなく、それはMYSQL

予想される出力:私はこのクエリを試みたが、ブロブを返し、私はない

subsid | debitamt  | debitcredit  | amount  | 
1  | 200   | null    | 100  | 
2  | null   | 500    | 300  | 
3  | 100   | 200    | 300  | 

それが正しいかどうかを知る。

SELECT 
IF(debitamt = "debitamt", 0, debitamt) as debitamt, 
IF(debitcredit = "debitcredit", 0, debitcredit) as debitcredit, 
IF(amount = "amount", 0, amount) as amount 
from (
    SELECT a.subsid, sum(debitamt) as debitamt, "debitcredit", "amount" FROM 
    new.billing a 
    UNION ALL 
    SELECT b.subsid, "debitamt", sum(debit-credit) as debitcredit, "amount" 
    FROM new.debitcredit b 
    UNION ALL 
    SELECT c.subsid, "debitamt", "debitcredit", sum(amount) as amount FROM 
    new.jso c 
) a group by a.subsid 

ありがとうございました。

答えて

0

これを試してみては...あなたの内側のフィールドを選択し、表示

SELECT 
a.subsid, 
sum(debitamt) as debitamt, 
sum(debitcredit) as debitcredit, 
sum(amount) as amount 
from (
    SELECT a.subsid, sum(debitamt) as debitamt, 0 as debitcredit,0 as amount --only need the column defined in first union FROM 
    new.billing a 
    group by a.subsid 
    UNION ALL 
    SELECT b.subsid,0 , sum(debit-credit) ,0 
    FROM new.debitcredit b 
    group by b.subsid 
    UNION ALL 
    SELECT c.subsid, 0, 0, sum(amount) FROM 
    new.jso c 
    group by c.subsid 
) a group by a.subsid 
+0

を修正ありがとうございました。それは働いている:) –

関連する問題