I have a question about aggregating numbers.
Suppose I have a SALES table that whose records are at an item transaction level (i.e. it displays sales by item).
I also have an ITEM table that contains item dept_nbr and sub_cat_nbr.
There is a DEPT table that maps dept_nbr to dept_desc, and a SUB_CAT table that maps sub_cat_nbr to sub_cat_desc.
Question: Suppose I want to find the total sales listed by department and subcategory descriptions. How can I best write the SQL? I don't want to see sales at the item level, only on the department and subcategory levels.
SALES table columns: sales_nbr, item_nbr, sales$
ITEM table columns: item_nbr, dept_nbr, sub_cat_nbr
DEPT table columns: dept_nbr, dept_desc
SUB_CAT table columns: sub_cat_nbr, sub_cat_desc
Would this work?
SELECT distinct
dept.dept_desc,
sub_cat.sub_cat_desc,
SUM(sales.sales$)
FROM
dept, sub_cat, item, sales
WHERE
dept.dept_nbr = item.dept_nbr
AND sub_cat.sub_cat_nbr = item.sub_cat_nbr
AND item.item_nbr = sales.item_nbr