0

Continue my last question before. But now I add some condition below:

As per working query code:

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 = 'DP0002'
GROUP BY R.budgetid_fk;

On that code, It will get the total SUM of price from some column.

requestid      | budgetid_fk    | category  | itemid_fk | quantity | currencyid_fk | price | discount | userid_fk
RQ201803000001 | BU201803000002 | Item      | IT0001    |          |       |
RQ201803000002 | BU201803000002 | Project   |           | 20       | CU0002 | 750 | 10 | US0004

Now I need to add extra code to calculate Category Project(RQ201803000002)

price accumulate with

SUM(R.quantity * I.price * COALESCE(CC.amount,1)) as total

Logic:

(quantity * (price * currency)) - discount as total2

*need to convert the currency first
*get department from userid_fk

Then accumulate it

total + total2

You can see the sql fiddle here

2
  • what the unit of discount ? (do we need to change currency too ?) Commented Mar 12, 2018 at 10:55
  • What your request so far? Explain exactly how to get the discount and where you're stuck. Commented Mar 12, 2018 at 13:44

1 Answer 1

0

I think you just want this (apart if the discount come form another table ?):

SUM(R.quantity * (I.price - discount) * COALESCE(CC.amount,1)) as total
Sign up to request clarification or add additional context in comments.

1 Comment

Please have a look on table tb_pro_request the price is not getting from tb_items, but from same table tb_pro_request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.