Search code examples
sql-server-2008coldfusioncoldfusion-9

How to calculate a price from a database column


I am trying to calculate the overall price from a database column called "price". The idea is that users place an order, which is recorded in the orders table. An admin then comes along and can view the total profit made on all the orders.

I have no idea how to do this. I think I need to put it into a for ... loop and add the results on top of each other. But I am not sure where to start. (I have been using ColdFusion for maybe 2 days now, so any and all help is appreciated.).

EDIT - Further info, I am using SQL server 2008 with sqlsrv and ColdFusion 9. After double checking the database its not price it's "orderValue".

EDIT - More further info, Thanks for all the help and what not, about the total vat, etc that i mentioned in another comment, i want to do this myself, this is a starting point and it is enough to tick the last requirements box of this shopping site for uni, however i want to progress it after ive handed it in tomorrow (deadline) and i was more stating my further plans than asking for help, again thank you guys for all the help really a great community here.


Solution

  • You'll want to do this in SQL, not ColdFusion:

    <cfquery name="totalSalesQuery">
        SELECT SUM(price) AS totalSales
        FROM orders
    </cfquery>
    
    <cfoutput>#totalSalesQuery.totalSales#</cfoutput>
    

    Also...

    You won't be able to calculate profit from the price column in the orders table. You'd need to have a way of subtracting out the cost of each order to determine profit. So I assume you're actually looking for total sales.

    If you want to format the result as US dollars, you can wrap your output in a formatting function:

    <cfoutput>#dollarFormat(totalSalesQuery.totalSales)#</cfoutput>