0

I have a table named sales with 5 fields:

  • id
  • date
  • id_store
  • id_customer
  • price

The table customers:

  • customers.id
  • customers.name

And the table stores:

  • stores.id
  • stores.name

I want to get this result:

January, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
February, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
March, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
April...

is it possible?

1
  • When you say "total customer name1", do you mean "total sales price of the customer name1"? If yes, you could use the "GROUP BY" statement mixed with the option "SUM". Commented May 6, 2013 at 16:31

1 Answer 1

1

It is possible, using JOINs and WITH ROLLUP. Basically, you'll JOIN your tables together to get the data rows you need, then GROUP BY multiple columns (in your case, probably GROUP BY MONTH(sales.date), stores.id, customers.id WITH ROLLUP). Grouping by multiple columns gives you nested groups, in the order the columns are listed. WITH ROLLUP will give you summary data for each nesting level; so in my example, you'd get totals per customer per store per month, and totals per store per month, and overall totals per month.

Sign up to request clarification or add additional context in comments.

2 Comments

But show a column with total for each customer and each store?
It will show totals for each customer by store & month, and for each store by month. If you want independent totals for each, you'll have to run independent queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.