我有以下03个MySQL表来维持不同投票下的分配,发行,裁减,退回和请求资金。
表,finance_vote
83 -116 -9 -98 115 -126 -3 -49
表,finance_budget
+---------+----------------+
| vote_id | vote |
+---------+----------------+
| 1 | 320-3-5-0-1001 |
| 2 | 320-3-5-0-1002 |
| 3 | 320-3-5-0-1003 |
| 4 | 320-3-5-0-1004 |
+---------+----------------+
表,finance_request_budget
+---------+--------+---------------+--------+
| vote_id | amount | budget_status | status |
+---------+--------+---------------+--------+
| 1 | 50000 | allocation | 1 |
| 2 | 75000 | allocation | 1 |
| 3 | 100000 | allocation | 1 |
| 1 | -22000 | issues | 1 |
| 4 | 800000 | allocation | 1 |
| 4 | -90000 | issues | 1 |
| 4 | 43000 | cut | 1 |
+---------+--------+---------------+--------+
01)分配的资金在finance_budget表格中的budget_status字段中显示为“分配”,发行资金作为“发行”并将资金削减为“削减”。
02)请求finance_request_budget表中字段,budget_status下的资金为“request_allocation”,并将资金返回为“return_allocation”。
03)然后我需要使用以下公式生成以下输出。
balance = allocation - issues + cut
我使用以下查询来执行此操作
+---------+--------+--------------------+
| vote_id | amount | budget_status |
+---------+--------+--------------------+
| 1 | 10000 | request_allocation |
| 2 | 25000 | request_allocation |
| 1 | 8000 | return_allocation |
| 3 | 24000 | return_allocation |
| 3 | 60000 | request_allocation |
+---------+--------+--------------------+
04)然后我得到了以下输出,它是正确的
SELECT
vote,
SUM(allocation) AS allocation,
SUM(issues) AS issues,
SUM(cut) AS cut,
SUM(balance) AS balance
FROM (
SELECT
vote,
CASE WHEN finance_budget.budget_status = 'allocation' THEN finance_budget.amount else 0 end AS allocation,
CASE WHEN finance_budget.budget_status ='issues' AND transfer_status='Approved' THEN finance_budget.amount else 0 end AS issues,
CASE WHEN finance_budget.budget_status = 'cut' THEN finance_budget.amount else 0 end as cut,
CASE WHEN finance_budget.status != 0 THEN finance_budget.amount else 0 end as balance
from finance_budget
left join finance_vote on finance_budget.vote_id=finance_vote.vote_id
where finance_budget.status=1
)tmp
group by vote
05)但是我需要在以下条件下将返回的分配添加到输出中。
balance = allocation - issues + cut + return_allocation
因此,我按如下方式编辑了查询。
+----------------+------------------+---------------+------------+-------------+
| Vote | Allocated Amount | Issued Amount | Cut Amount | Current_Balance |
+----------------+------------------+---------------+------------+-------------+
| 320-3-5-0-1001 | 50000 | 22000 | 0 | 28000 |
| 320-3-5-0-1002 | 75000 | 0 | 0 | 75000 |
| 320-3-5-0-1003 | 100000 | 0 | 0 | 100000 |
| 320-3-5-0-1004 | 800000 | 90000 | 43000 | 753000 |
+----------------+------------------+---------------+------------+-------------+
06)之后我得到了以下输出,它的数据设置不正确如下。
SELECT
vote,
SUM(allocation) AS allocation,
SUM(issues) AS issues,
SUM(cut) AS cut,
SUM(return_allocation) AS return_allocation,
SUM(balance) AS balance
FROM (
SELECT
vote,
CASE WHEN finance_budget.budget_status = 'allocation' THEN finance_budget.amount else 0 end AS allocation,
CASE WHEN finance_budget.budget_status ='issues' AND transfer_status='Approved' THEN finance_budget.amount else 0 end AS issues,
CASE WHEN finance_budget.budget_status = 'cut' THEN finance_budget.amount else 0 end as cut,
CASE WHEN finance_request_budget.budget_status ='return_allocation' THEN finance_request_budget.amount else 0 end AS return_allocation,
CASE WHEN finance_budget.status != 0 THEN finance_budget.amount else 0 end as balance
from finance_budget
join finance_vote on finance_budget.vote_id=finance_vote.vote_id
join finance_request_budget on finance_request_budget.vote_id=finance_budget.vote_id
where finance_budget.status=1
)tmp
group by vote
07)我无法理解我的错误。任何人都可以帮助我吗?