views:

75

answers:

1

How would I do a basic profit and loss statement in SQL?

I have a sales table with a costs field, and summing that field should show the total profit

Likewise I have a purchases table with a cost field, which should show the total money going out.

To show profit, would I just SUM the total costs in sales, and subtract the total costs in purchases?

+2  A: 

Maybe this will work?

SELECT Q1.total_sales - Q2.total_purchases AS NET
FROM
(select SUM(sales.costs) AS total_sales
from sales) Q1,
(select SUM(purchases.costs) AS total_purchases
from purchases) Q2

Could you give more details on your table structure?

FrustratedWithFormsDesigner
@OMG Ponies: Yeah, you're right. I think it's fixed now, using separate subqueries for the two sums.
FrustratedWithFormsDesigner
It´s as I said in the question, I only have a sales and a purchases table, and each has a costs column.
Jacob