2016-12-03 5 views
0

総配分、カット配分を印刷する必要があります。&ネット配分は、関連する投票の下でさまざまなオフィスで発行されました。私は次の表を使用しました。グロス、カット&ネット金額のMYSQL連合オペレーション

+--------+------------+-----------+------------+ 
| v_code | Gross | Cut | Net  | 
+--------+------------+-----------+------------+ 
|  1 | 200,000.00 | 68,000.00 | 132,000.00 | 
|  1 | 80,000.00 | 50,000.00 | 30,000.00 | 
|  2 | 40,000.00 | 0.00  | 40,000.00 | 
|  3 | 150,000.00 | 75,000.00 | 75,000.00 | 
+--------+------------+-----------+------------+ 

02)が、私はその出力

select `vote`.`vote` AS `vote`,`office`.`office` AS `office`, 
    `issues_tot`.`amount` AS `Gross`, 
    coalesce(`cp_tot`.`amount`,0) AS `Cut`, 
    (`issues_tot`.`amount` - coalesce(`cp_tot`.`amount`,0)) AS `Net` 
    from (((`vote` join `issues_tot` on((`vote`.`v_code` = `issues_tot`.`v_code`))) join 
`office` on((`office`.`oid` = `issues_tot`.`oid`))) left join 
`cp_tot` on((`issues_tot`.`v_code` = `cp_tot`.`v_code`))) 

を生成するために、次のスクリプトを使用ししかし、それは繰り返されたレコードと次の出力が生成されます:

1) Total issues (Gross Allocation) are in the Table, Named "issues_tot" 
+---------+------+------------+ 
| v_code | oid | amount | 
+---------+------+------------+ 
|  1 | 2 | 200,000.00 | 
|  1 | 3 | 80,000.00 | 
|  2 | 1 | 40,000.00 | 
|  3 | 2 | 150,000.00 | 
+---------+------+------------+ 

2) Cut amounts (Cut Allocation) are in the Table, Named "cp_tot" 
+--------+-----+-----------+ 
| v_code | oid | amount | 
+--------+-----+-----------+ 
|  1 | 2 | 68,000.00 | 
|  1 | 3 | 50,000.00 | 
|  3 | 2 | 75,000.00 | 
+--------+-----+-----------+ 

3) Table, Named "vote" 
+--------+-------------------------+ 
| v_code |   vote   | 
+--------+-------------------------+ 
|  1 | 001-2-6-3-2502   | 
|  2 | 001-1-4-21-2202   | 
|  3 | 101-1-2-0-1405   | 
+--------+-------------------------+ 

4) Table, Named "office" 

+-----+----------------------+ 
| oid |  office  | 
+-----+----------------------+ 
| 1 | Weeraketiya   | 
| 2 | Tissamaharama  | 
| 3 | District Sec | 
+-----+----------------------+ 

そして、所望の出力は次のように

+------------+----------------+--------------+-------------+--------------+ 
| Vote   | Office | Gross  |  Cut  |  Net  | 
+---------------+-------------+--------------+-------------+--------------+ 
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 68,000.00 |132,000.00 | 
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 50,000.00 | 150,000.00 | 
| 001-2-6-3-2502| District Sec | 80,000.00 | 68,000.00 | 12,000.00 | 
| 001-2-6-3-2502| District Sec | 80,000.00 | 50,000.00 | 30,000.00 | 
| 001-1-4-21-2202| Weeraketiya | 40,000.00 | -   | 40,000.00 | 
| 101-1-2-0-1405 | Tissamaharama | 150,000.00 | 75,000.00 | 75,000.00 | 
+------------+-----------------+--------------+-------------+--------------+ 

何がうまくいかないのか分かりません。誰でも助けてくれますか?

答えて

1

まず、次のクエリは、あなたが望む結果を得るでしょう:

select 
    v.vote, 
    o.office, 
    it.amount as Gross, 
    coalesce(ct.amount , 0) as Cut, 
    it.amount - coalesce(ct.amount, 0) as Net 
from issues_tot it 
left join cp_tot ct 
on it.v_code = ct.v_code 
and it.oid = ct.oid 
left join vote v 
on it.v_code = v.v_code 
left join office o 
on it.oid = o.oid 
order by it.v_code 

SQLFiddle Demo Here、唯一の問題は、あなたがissues_totcp_totoidと一致することを忘れていることです。
oid基準が存在しない場合、試合は次のように行います:ので、あなたのクエリで6つのレコードがそれ

# issues_tot      # cp_tot 
| v_code | oid | amount |  | v_code | oid | amount |    
+---------+------+------------+  +--------+-----+-----------+ 
|  1 | 2 | 200,000.00 | -> |  1 | 2 | 68,000.00 | 
            |  1 | 3 | 50,000.00 |         
|  1 | 3 | 80,000.00 | -> |  1 | 2 | 68,000.00 | 
            |  1 | 3 | 50,000.00 | 
|  2 | 1 | 40,000.00 | -> no record match 
|  3 | 2 | 150,000.00 | -> |  3 | 2 | 75,000.00 | 

です。

関連する問題