tags:

views:

53

answers:

3

I have a sales table, with fields product and cost of type varchar and decimal.

I also have a purchases table, with fields product and cost, also of type varchar and decimal.

lastly, I have a products table, with fields name, category and cost, of type varchar, varchar and decimal.

I am doing the following query to get the NET income

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

How can I relate it back to products in the products table, to only select certain products where category = blah?

Would this have to be a union?

I would also like to show this for the current year or month, to do so would I select the date field and just append

WHERE MONTH( s.saledate ) = MONTH( NOW( ) )

to my query?

+1  A: 

Something like this...?

 SELECT Q1.total_sales - Q2.total_purchases AS NET
        FROM (select SUM(sales.cost) AS total_sales, product
        from sales) Q1, (select SUM(purchases.cost) AS total_purchases
        from purchases) Q2 
    INNER JOIN Products on Q1.Product = Products.Product
    WHERE Products.Category = "blah"
froadie
You'll get an error for the join - there's no `q1.product` column in the derived table/inline view, only the SUM column
OMG Ponies
hmm, I get Unknown column 'Q1.Product' in 'on clause' when trying that, but it looks like it should be perfect
Jacob
@OMG Ponies - ah, true. I'll add product into the Q1 subselect
froadie
@Jacob - check the edit
froadie
@froadie, same error...
Jacob
+1  A: 

Use:

SELECT Q1.total_sales - Q2.total_purchases AS NET
  FROM (SELECT SUM(s.cost) AS total_sales
          FROM SALES s
          JOIN PRODUCTS p ON p.product = s.product
                         AND p.category = ?
         WHERE MONTH(s.saledate) = MONTH(NOW()) ) Q1, 
   (SELECT SUM(p.cost) AS total_purchases
      FROM PURCHASES p) Q2 
OMG Ponies
I don´t want the purchases table mapped to the products table at all, as the purchases are for outside things which are not in the products table. Could I just remove the entire second subquery?
Jacob
@Jacob: That's fine to remove the JOIN on the second subquery, assuming you don't care about the category filteration in there. Unless you have another way of doing it?
OMG Ponies
I don´t have another way....but the products table only has information for things we sell, the purchases table can contain anything....because who knows what we may have to purchase....if that makes sense
Jacob
I am trying the following : SELECT Q1.total_sales - Q2.total_purchases AS NET FROM (select SUM(sales.cost) AS total_sales FROM SALES s JOIN PRODUCTS p ON p.name = s.product WHERE p.category = 'food') Q1, (select SUM(purchases.cost) AS total_purchases FROM PURCHASES p) Q2 - and oddly enough I get unknown column 'sales.cost' in 'field list'
Jacob
@Jacob: I get what you're saying. A LEFT JOIN wouldn't help -- the category criteria would render it to be an INNER JOIN :/
OMG Ponies
@Jacob: The error is because I didn't update the table alias - correct my answer.
OMG Ponies
That works fine, thankyou! I am trying also to add a date clause, as you can see at the end of my editied question. I tried putting this afer "from sales s" but it did not work, must I also select saledate from the table?
Jacob
@Jacob: Gotcha, see updated answer. I moved the category check into the JOIN criteria.
OMG Ponies
perfect, thankyou once again.
Jacob
Yes certainly an upvote...I still have to remember to do that, ill go back and see if I forgot any.
Jacob
A: 

Add this to the end of your query

JOIN products AS p ON p.id = Q1.id WHERE p.category = @blah

Kyra