tags:

views:

52

answers:

2

I have three tables products, sales and costs.

The sales and products table each have a costs field of type decimal and a date field of type date.

Additionally, the sales table has a productname field, which is matched to a productname field in the products tables.

The products table also has a category field.

I want to select all sales and all purchases for each month, and display them in separate columns grouped by month, up until the current year.

Something like the following

            Sales  Purchases

January      100      50
Febuary      100      50
March        100      50
April        100      50
May          100      50
June         100      50
July          30      50

The purchases table is not related to any other table.

However, the sales table productname field only contains products that exist in the products.productname.

I want to add on to the end of my query

WHERE sales.productname=products.productname AND category='Food'

I am unsure how to return multiple columns, sum them for each month, and then group them by month.

I asked something similar here, and took a solution however it returned sales and purchases together in one column.

How would I keep them as separate columns?

+1  A: 

Try this, or something similar...

SELECT sal.theMonth, sal.sumSales, pur.sumPurchases
FROM
 (  
   SELECT date_format(theDate, "%Y-%m") AS theMonth, sum(sales) AS sumSales
   FROM sales_table
   GROUP BY theMonth
 ) AS sal
INNER JOIN
 (  
   SELECT date_format(theDate, "%Y-%m") AS theMonth, sum(purchases) AS sumPurchases
   FROM purchases_table
   GROUP BY theMonth
 ) AS pur
ON sal.theMonth = pur.theMonth

Clearly you will need to adjust table and field names as appropriate for you table definitions...

Dave Rix
You may also want to look at my answer on http://stackoverflow.com/questions/3093924 which will allow you to get results even for months without any data in your tables.The above answer assumes you have results in both tables for every month...
Dave Rix
Hmm, I was just about to ask for that. However, I don´t have the possibility to create more tables easier, or at least I would rather not as I am working with a CMS sort of system. Is there a way to show sales or purchases for any month, as long as either sales or purchases has data? Also, perhaps a dumb question, but would I just put my WHERE clause before the GROUP BY statements?
Jacob
yeup, put any filters within the sub-queries, as it will restrict the data before grouping it together.
Dave Rix
+2  A: 

Somewhat similar to Dave's answer but will show both Sales and Purchases for months where either exists:

SELECT theMonth, sum(Sales) as sumSales, sum(Purchases) as sumPurchases
FROM
 ( SELECT date_format(theDate, "%Y-%m") AS theMonth, Cost as Sales, 0 AS Purchases
   FROM sales, products
   WHERE sales.productname=products.productname AND category='Food'
  UNION ALL
   SELECT date_format(theDate, "%Y-%m") AS theMonth, 0 as Sales, Cost as Purchases
   FROM purchases
 ) AS all_costs
group by theMonth;

(Edited following comments)

Mark Bannister
Hi Mark, thanks a lot for your answer. I am having trouble changing to suit my needs(possibly because I am not understanding it). Both my sales and purchases table have fields called cost, and neither have fields called sales or purchases. From what I can tell, your solution assumes this?
Jacob
Hi Jacob, assuming it is cost from the sales table you want to report as the sales value, and cost from the purchases table you want to report as purchases, see the edited answer.
Mark Bannister
+1! Looks good to me Mark, although what happens if the sames and purchases tables get really big (really, really, big!) yours is selecting all data from both, instead of grouping by month within the query... I do reckon this can be done, but can't quite see how.Adding a table to the db structure for the left-join on months would be sooo much easier!
Dave Rix
Hi Dave, your query selects all data from each table, summarises them by month and then joins them on a month by month basis, while mine takes all data from both tables, unions them and then summarises them by month. I would expect the performance to be similar in either case, but I'm not an expert on MySQL. My query took into account the fact that Jacob is not in a position to add new tables easily; adding a new table would, of course, also enable the query to return months with no data for either sales or purchases.
Mark Bannister
Hi Mark, I still get the error that cost is ambiguous...should I use sales.cost and purchases.cost instead?
Jacob
Hi Jacob, you've probably got a Cost column on your Product table as well - so yes.
Mark Bannister