MySQL SELECT SUM基于另一个表

问题描述 投票:1回答:2

我在MySQL上有一张桌子

tb_currencies

currencyid | currency_name
CU0001     | IDR
CU0002     | SGD
CU0003     | USD

tb_currency_converters

currencyconverterid | currency_month | from_currencyid_fk | to_currencyid_fk | amount
CC0001              | 2018-03-01     | CU0001             | CU0002           | 0.00009
CC0002              | 2018-03-01     | CU0002             | CU0001           | 10425
CC0003              | 2018-03-01     | CU0003             | CU0002           | 1.31964

tb_budgets

budgetid       | budget_month | departmentid_fk | currencyid_fk
BU201803000001 | 2018-03-01   | DP0003          | CU0002
BU201803000002 | 2018-03-01   | DP0002          | CU0002

tb_items

itemid | item_name | currencyid_fk | price
IT0001 | Mouse     | CU0001        | 165000
IT0002 | Keyboard  | CU0002        | 20
IT0003 | TV LCD    | CU0003        | 350

tb_pro_request

requestid      | budgetid_fk    | itemid_fk | quantity
RQ201803000001 | BU201803000001 | IT0002    | 1
RQ201803000002 | BU201803000001 | IT0003    | 5
RQ201803000003 | BU201803000001 | IT0004    | 1

例:

我的departmentid_fk是:DP0003,意味着我有货币SGD预算。

在tb_pro_request上,有两个项目交易不同的货币(IDR到USD)与预算部门货币(SGD)。

我想要的是,具有不同货币的2个项目需要首先转换为SGD(由于我的部门预算是:SGD)然后SUM它。

*逻辑,如果货币是SGD然后没有必要转换,但如果不是SGD然后转换它首先使用tb_currency_converters然后SUM它。

可以直接查询吗?

我的查询代码到目前为止:

SELECT
    R.requestid,
    R.budgetid_fk,
    R.category,
    R.itemid_fk,
    SUM(R.quantity),
    R.request_date,
    R.investmentid_fk,
    R.remarks,
    R.approval_status,
    I.itemid,
    I.item_name,
    I.price,
    SUM(R.quantity) * I.price as total, 
    B.budgetid,
    B.budget_month
FROM
    tb_pro_request R,
    tb_items I,
    tb_budgets B
WHERE
    R.itemid_fk = I.itemid AND
    R.budgetid_fk = B.budgetid AND
    R.investmentid_fk = '' AND
    B.active = 'Y' AND
    (R.approval_status = 'P' OR R.approval_status = 'A') AND
    DATE_FORMAT(B.budget_month,'%Y-%m') = '2018-03' AND
    B.departmentid_fk = 'DP0003'
    GROUP BY I.currencyid_fk

您可以看到下面有3种不同货币的商品。我想要的,转换为SGD货币IDR,USD。然后使它成为(1行)enter image description here

mysql sql group-by
2个回答
1
投票

我觉得你在你的FK中有点陷入困境,但至少你有精神;)

SQL Fiddle

MySQL 5.6架构设置:

查询1:

SELECT
    R.budgetid_fk,
    SUM(R.quantity),
    SUM(R.quantity * I.price * COALESCE(CC.amount,1)) as total, 
    B.budgetid,
    B.budget_month
FROM tb_pro_request R 
INNER JOIN tb_items I 
  ON R.itemid_fk = I.itemid
INNER JOIN tb_budgets B 
  ON R.budgetid_fk = B.budgetid 
  AND B.active = 'Y'
LEFT JOIN tb_currency_converters CC 
  ON CC.from_currencyid_fk = I.currencyid_fk 
  AND CC.to_currencyid_fk = B.currencyid_fk
WHERE
    R.investmentid_fk = '' 
    AND (
      R.approval_status = 'P' 
      OR R.approval_status = 'A'
    ) 
    AND DATE_FORMAT(B.budget_month,'%Y-%m') = '2018-03' 
    AND B.departmentid_fk = 'DP0003'
GROUP BY R.budgetid_fk

Results

|    budgetid_fk | SUM(R.quantity) |             total |       budgetid | budget_month |
|----------------|-----------------|-------------------|----------------|--------------|
| BU201803000001 |               7 | 575.2840143424692 | BU201803000001 |   2018-03-01 |

1
投票

您的查询是无效的SQL,因为您正在聚合数据并仍显示未聚合的列,例如requestid。哪个requestid?可能有几个。

以下组按预算货币计算。如果department_fk加上budget_month在表tb_budgets中是唯一的,那么我们只选择一个,因此可以删除GROUP BY子句。

select
  sum(i.price * r.quantity * coaleasce(cc.amount, 1)) as total,
  c.currency_name
from tb_budgets b 
join tb_pro_request r on  r.budgetid_fk = b.budgetid 
                      and r.approval_status in ('P','A')
                      and r.investmentid_fk = ''
join tb_items i on i.itemid = r.itemid_fk
join tb_currencies c on c.currencyid = b.currencyid_fk
left join tb_currency_converters cc on  cc.currency_month     = b.budget_month
                                    and cc.from_currencyid_fk = i.currencyid_fk
                                    and cc.to_currencyid_fk   = b.currencyid_fk
where b.departmentid_fk = 'DP0003'
and date_format(b.budget_month,'%Y-%m') = '2018-03'
and b.active = 'Y'
group by c.currency_name;
© www.soinside.com 2019 - 2024. All rights reserved.