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?
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.
Related
I have three queries;
Q1. Returns all customer records (id, name, year amount) where the customer has spent money with us.
Q2. Returns all customers records (id,name, year of regis, number) where the customer has a telephone number.
Q3. Returns all customer records (id,name, year, amount) where the customer has a purchased a specific product.
I want a new query that will return a list of all customers ids, names if they appear in any of the 3 queries, along with the latest year. How do I do that?
First, add all the results of the 3 queries together using UNION, then group the records by id and select the first name (or any name really, because they must be all the same) and the maximum year.
Try this:
SELECT id, FIRST(name), MAX(year)
FROM (
SELECT id, name, year FROM q1
UNION
SELECT id, name, year FROM q2
UNION
SELECT id, name, year FROM q3) AS q
GROUP BY id
i have a sales table which contain customer_id time_id
i want to count the products bought by a customer at one time_id.
i am running query
select customer_id,time_id,count(time_id) as count from sales where customer_id=2094 group by time_id.
this query runs and give the result but i have different customer_ids in another customer table then i run this query
select customer_id,time_id,count(time_id) as count from sales where customer_id in (select customer_id from customer) group by time_id.
but it is not showing exact result.
I'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups
I'm trying to join 3 tables for a system to keep track of what comes in and out of my shop.
The first tables is as following. (Production)
id
Amount
Date
item id
next table is: (sales)
id
date
amount
customer id
amount
the last is: (waste)
id
date
amount
reason
I haven't really found a way to join those 3 tables without using a common value they could join on. I need to order them by date so I can see the history of my income and expenses. If the 3 different tables could come with a individual value like 3 for waste 2 for sales and 1 for production would also be great.
What you want is not a join of the tables but a union: http://dev.mysql.com/doc/refman/5.0/en/union.html
Something like
(SELECT 'production' as source, id, Amount AS amount, Date AS date FROM Production)
UNION
(SELECT 'sales' as source, id, amount, date FROM sales)
UNION
(SELECT 'waste' as source, id, amount, date FROM waste)
ORDER BY date;
I'm using an (OLD) MySQL 4.0 database and I need to sum quantities when for two rows, a specific column has the same value.
Example,
So let,s say I have these columns:
doc_nr | client_name | article_id | qty
When they are inserted into the database, they are separated into different rows each with quantity 1 (complicated amount of services control background for upper management)
What I want to do is that, for the same article_id, and for the same doc_nr (which imply that it's for the same customer) if the article_id is the same, I want to output the quantity as a SUM of them.
So, for a customer which ordered a quantity of 2 articles, that is then inserted as two separate rows with quantity 1, how do I output quantity 2 again?
You could use a GROUP BY query:
SELECT doc_nr, client_name, article_id, SUM(qty) AS qty
FROM yourtable
GROUP BY doc_nr, client_name, article_id
if qty is always 1, instead of SUM(qty) you could just use COUNT(*) that counts the number of grouped rows.