tags:

views:

74

answers:

4

I have a products table, with the fields productname, category and cost, of type varchar, varchar and double.

I then have a sales table, with the fields productname, cost, and saledate, of type varchar, double and date.

Lastly, I have a purchases table with the fields purchase, cost and purchasedate, of type varchar, double and date.

I want to calculate the number of sales for certain dates, for certain categories.

I have at the moment this query to show the monthly totals for a year:

SELECT month(saledate), SUM(cost)
FROM sales
GROUP BY month(saledate)
UNION ALL
SELECT month(purchasedate), SUM(cost)
FROM purchases
GROUP BY month(purchasedate)

How could I restrict it to certain categories?

To define certain categories from products, such as food and drink, and only include products in these categories in my calculation?

+1  A: 

just add a WHERE clause like this one:

[...] WHERE products.category = 'food' OR prodcuts.category = 'drink' [...]

Daniel
+4  A: 
SELECT month(saledate), SUM(sales.cost)
FROM sales, products
WHERE sales.productname=products.productname
AND category='food'
GROUP BY month(saledate)
UNION ALL
SELECT month(purchasedate), SUM(purchases.cost)
FROM purchases, products
WHERE purchases.productname=products.productname
AND category='food'
GROUP BY month(purchasedate)

The table needs to be part of the SQL statement in order to restrict the result-set rows based on some value held in it.

Borealid
perfect...just a quick question, but to do OR in sql, would just be category='food' || 'beer' || 'smokes'...?
Jacob
Use the word OR. category='food' OR category='beer'.
Borealid
Also, note that Jun1st below is right: the categories are actually **excluded** from the resultant total when you use this query.
Borealid
+1  A: 

I'd be inclined to use a subquery rather than a join, and I think you want it in both parts of the union. But Borealid's answer is quite sound. The difference is a matter of taste.

SELECT month(saledate),
       SUM(sales.cost)
    FROM sales, products
    WHERE EXISTS (SELECT *
                  FROM products
                  WHERE purchases.productname = products.productname AND
                        category              = 'food')
    GROUP BY month(saledate)
UNION ALL
SELECT month(purchasedate),
       SUM(purchases.cost)
    FROM purchases, products
    WHERE EXISTS (SELECT *
                      FROM products
                      WHERE purchases.productname = products.productname AND
                            category              = 'food')
    GROUP BY month(purchasedate)
Brian Hooper
Jacob
You actually have an example of performing an AND in Mr. Hooper's answer there. And no, there's no real difference between the subqueries and the joins in this case. In general, one must watch out for explosive JOINs and inefficient/unnecessary subqueries, which are different performance demons, but here the only difference is readability. Choose whichever one you think is more readily comprehensible.
Borealid
Brian Hooper
Thankyou both for the detailed explanations.
Jacob
+1  A: 

If you want your calculation result doesn't include these categories, you should use WHERE.

otherwise, using HAVING

Jun1st