List customers for each category and the total of order placed by that customer in a given category. In the query show three columnm: CategoryName, CustomerName, and TotalOrders (which is price * quantity for orders for a given customer in a given category). Sort this data in descending order by TotalOrders.
should be this ...
select category, customer, sum(price*quantity) as TotalOrders
from yourTable
group by category, customer
order by TotalOrders desc;
Related
Basically, I'm using a database of a restaurant, I need the code to list off all the food bought (name, category, price, amount etc), but only from the bill that has the highest total price. I tried using sum(max()) to get the total price, but it would show error 1111 which is invalid use of group function, I tried to look up fixes but none of the results were helpful
I feel like it's also important to note that all_data is actually a view.
Select name, category, amount, if(amount>1, price*amount,price)
From all_data
Where (select sum(if(amount>1,amount*price,price)) from all_data)=(select max(sum(if(amount>1,amount*price,price))) from all_data)
You can do it using inner join to a subquery where we can get all the food bought that has the highest total price :
select t.*, s.total
from all_data t
inner join (
Select name, category, amount, price, sum(price*amount) as total
From all_data
where amount > 1
group by name, category
) as s on s.name = t.name and s.category = t.category
order by s.total desc;
if you want only 10 products add limit 10 to the subquery
I need to get the customer state, book name, order count and revenue from tables named order_line_items (contains the order quantity), orders (contains the revenue), customers (contains the state) and books (contains book name) . So far I have:
SELECT
YEAR(purchased_at) AS year_purchased,
COUNT(*) order_count,
COUNT(DISTINCT customer_id) customer_count,
CONCAT('$', FORMAT(SUM(total), 2)) AS revenue
FROM
orders
GROUP BY
year_purchased
ORDER BY
year_purchased;
the code above works fine but is not complete, when I add the rest of what I need I get a wrong total for count and revenue. This is giving me issues:
SELECT
YEAR(purchased_at) AS year_purchased,
COUNT(*) order_count,
COUNT(DISTINCT customer_id) customer_count,
CONCAT('$', FORMAT(SUM(total), 2)) AS revenue,
state,
collection
FROM
customers c
JOIN books
JOIN orders o ON c.id = o.customer_id
GROUP BY
year_purchased,
state,
collection
ORDER BY
year_purchased,
state,
collection LIMIT 4;
So I have the following 3 tables:
Table: Products
Columns: id, name, description, price, currency
Table: Orders
Columns: id, firstName, lastName, phoneNumber
Table: Order_Products
Columns: orderId, productId, quantity
Now I'm trying to figure out where to put the total price of the order and I have 2 ideas:
Add a totalPrice column to the Orders table that will contain the sum of the price * quantity of all products in the order, or:
Add a price column to the Order_Products table that will contain the the price * quantity of that specific product, and then I'd have to get all Order_Products records for that order and sum their price columns.
I'm not quite sure which option is better, hence why I'm asking for recommendations here.
I would recommend that you store the order total in the orders table.
Why? Basically, order totals are not necessarily the same as the sum of all the prices on the items:
The prices might change over time.
The order itself might have discounts.
In addition, the order might have additional charges:
Discounts applied to the entire order.
Taxes.
Delivery charges.
For these reasons, I think it is safer to store financial information on the order when the order is placed.
I woulnd't recommend storing this. This is derived information, that can be computed on the fly whenever needed. If you are going to do the computation often, you can use a view:
create view orders_view as
select o.*, sum(p.price * op.quantity) total_price
from orders o
inner join order_products op on op.orderid = o.id
inner join products p on p.id = op.productid
group by o.id
I have table company. It stores the following data: id, title, budget, pid (parent id), total amount. It is necessary to calculate the company's budget + budget of subsidiaries. I do it in the column total.
"SELECT o.id, o.title, o.budget, o.pid,
(select sum(o1.budget)+o.budget from company o1 where o1.pid=o.id) total
FROM company o ORDER BY o.id";
This request summarizes the amount of budgets only on the level below, and I need to calculate the budgets of all the subordinate companies.
You are missing a GROUP BY, without the GROUP BY you will only get the total sum of your budget, try something like this:
SELECT o.id, o.title, o.budget, o.pid,
(select sum(o1.budget)+o.budget from company o1 where o1.pid=o.id) total
FROM company o
GROUP BY o.id
ORDER BY o.id
I want to list bestsellers
I have list of duplicates of sold products from table SALE and want to select those product properties from PRODUCT table
my query to find duplicates from table SALE is:
SELECT product, COUNT(*) AS COUNT
FROM sale
GROUP BY product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC
this query lists my bestsellers products, how to list these products properties from PRODUCT table in this order
In MySQL you can just join in the product table:
SELECT p.product, COUNT(*) AS COUNT
FROM product p JOIN
sale s
ON p.product = s.product
GROUP BY p.product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC ;
This assumes that p.product is the primary key on PRODUCT that links it to SALES. Also, are you just counting "orders" or do you have a "quantity" field that you also care about?