tags:

views:

822

answers:

3

How do you write a SQL query to calculate the VAT and then add it to Total?

Thank You

+3  A: 

Surely homework.

However, let us know what you've tried. In general, you're supposed to make an effort first before people on SO will help you.

What does your table look like? How is the total calculated? What query returns the total? What do you need to do to the total to add tge VAT?

Paul

Paul
A: 
SELECT SUM(value) as SubTotal,
   SUM(value * 0.15) as VAT,
   SUM(value) + SUM(value * 0.15) as GrandTotal
FROM <recordsource>
WHERE <filterclause>

The result is a single row recordset containing:

  • SubTotal. The total before VAT
  • VAT. The total amount of VAT to be paid
  • GrandTotal. The total amount to pay, including VAT.

The bits you will need to fill in are:

  • <recordsource> The tables and joins to get "value" from
  • value - the name of the field containing the pre-VAT price. This may be an expression that combines unit price with number of units.
  • <filterClause> - the filter to determine which values to add up.

Note that the last value, GrandTotal, can be calculated outsite the query, by simply adding SubTotal and VAT. Depending upon the application and it's use, there may also be a requirement to determine at a per-item level whether tax is to be levied or not (some items are legally except from tax).

If, however, you need an itemised invoice, you're better off returning each row, and calculating the VAT in your application code, so that you can output the item, unit cost, number of units, etc.

Jason Musgrove
As a follow up to this, if you're going to use this for a client, *please* put the VAT rate in a config file or similar. It will change (this year, in fact)
Brian Agnew
"The bits you will need to fill in are" ... and of course the table with VAT rates
Joe
Better yet: SUM(value) * 1.15 as GrandTotal
Eduardo Molteni
Thank you very much this worked!
-1: Wrong rounding rules for the calculation. No date factor (rates change). No allowance for exempt/zero-rated/... items (let alone items for which VAT only applied to part of the value).
Richard
A: 

the calculation very easy for the UP let the 1st steps as down

01) the Vale are as whatever 100 the vat amt has been fixed by the goverment suppose 15% the the formula apply sum(value*15) the total cost of the commidities add to the 15%