1

how to calculate two columns values in select query using mysql?

Here is my sql query

SELECT cusName, 
       remarks, 
       invoiceNo,
       invoiceDate, 
       total_VAT, 
       bill_tot,
       ROUND(((bill_tot - total_VAT) * 5/100),2) as vatamt,
       ROUND(bill_tot - total_VAT, 2) as exclvat                   
FROM invoices 
where invoiceDate between '2018-11-13' and '2018-11-13'
order by invoiceID;

In the above query i need to calculate sum value of vatamt and exclvat as total amount and display it.Can anyone help me please to do this?

0

3 Answers 3

1

You cannot reuse an aliased Calculated expression again inside a SELECT clause. They can be reused only in either GROUP BY, ORDER BY, HAVING clauses. You will need to specify the calculation expressions again, to calculate the total_amount:

SELECT cusName, 
       remarks, 
       invoiceNo,
       invoiceDate, 
       total_VAT, 
       bill_tot,
       ROUND((bill_tot - total_VAT) * 5/100,2) as vatamt, -- got rid of extra parentheses
       ROUND(bill_tot - total_VAT, 2) as exclvat,
       ROUND((bill_tot - total_VAT) * 5/100,2) + 
       ROUND(bill_tot - total_VAT, 2) as total_amount
FROM invoices 
where invoiceDate between '2018-11-13' and '2018-11-13'
order by invoiceID;
Sign up to request clarification or add additional context in comments.

12 Comments

is it possible to calculate all the sum of total_amount?
@blackwhite yes. just put this complete query under a FROM clause, and use SUM() over it. This is called derived tables. Refer this documentation for more understanding: dev.mysql.com/doc/refman/8.0/en/derived-tables.html
SELECT cusName, remarks, invoiceNo, invoiceDate, ROUND((bill_tot - total_VAT) * 5/100,2) as vatamt, ROUND(bill_tot - total_VAT, 2) as exclvat, ROUND((bill_tot - total_VAT) * 5/100,2) + ROUND(bill_tot - total_VAT, 2) as total_amount FROM (SELECT SUM(total_amount) AS sum_total_amount FROM invoices where invoiceDate between '2018-11-13' and '2018-11-13' order by invoiceID;
@blackwhite no. you are doing it other way around. your internal subquery should be outside instead.
i need to get total sum of three columns ie;vatamt,exclvat and total_amount
|
0

The answer given by @Madhur is probably the most performant. But in MySQL 8 we could make use of a CTE to avoid repeating common logic in the select clause:

WITH cte AS (
    SELECT *,
        ROUND(((bill_tot - total_VAT) * 5/100),2) AS vatamt,
        ROUND(bill_tot - total_VAT, 2) AS exclvat
    FROM invoices 
    WHERE invoiceDate = '2018-11-13'
)

SELECT
   cusName, 
   remarks, 
   invoiceNo,
   invoiceDate, 
   total_VAT, 
   bill_tot,
   vatamt,
   exclvat,
   vatamt + exclvat AS total
FROM cte
ORDER BY
    invoiceID;

3 Comments

Are CTEs materialized in MySQL ? I have never used them yet. How are they different in performance terms, compared to Derived Tables.
@MadhurBhaiya I believe CTEs are in fact materialized in MySQL, but they are only materialized once (contrast this with a view, which may be materialized multiple times, depending on the query).
Ok. version 5.7 and onwards MySQL optimized Derived tables and Views: dev.mysql.com/doc/refman/5.7/en/derived-table-optimization.html I believe that same should be applicable for CTEs.
0

If you wanted to rank readability over performance (although the performance impact might be negligible) you could also use a wrapping query:

SELECT cusName, 
       remarks, 
       invoiceNo,
       invoiceDate, 
       total_VAT, 
       bill_tot,
       vatamt,
       exclvat,
       vatampt + exclvat as total_amount
FROM (
    SELECT *,
           ROUND(((bill_tot - total_VAT) * 5/100),2) as vatamt,
           ROUND(bill_tot - total_VAT, 2) as exclvat                   
    FROM invoices 
    WHERE invoiceDate between '2018-11-13' and '2018-11-13'
    ORDER BY invoiceID
) a;

3 Comments

Just a note: MySQL version 5.7 and above only optimize the Derived tables. Previous versions materialize the subquery, which reduces performance.
@MadhurBhaiya am using MySQL Server 5.5.So shall i go with ur code?
@blackwhite yes in that case, my query would be most performant in terms of memory usage.

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.