I'm new here. First post. I'd really appreciate some help.
I'm trying to calculate total sales for all products sold.
I have a Quantity column and a Price column.
I understand how to multiply these two columns, BUT I do not know how to add them all together in the same query.
Here is an example:
Quantity: 2, 3, 1 Price: 2, 4, 5
I could do Quantity * Price to get: 4, 12, 5
But then how would I add 4 + 12 + 5 to get the total? I need this step to be included in the same query.
EDIT: Both the Quantity and Price columns are in the same table.
SALES (Quantity, Price)
I am using Microsoft SQL-Server.
Example if you have one table:
SELECT dbo.orderid,
SUM(dbo.quantity * dbo.price) AS grand_total,
FROM ORDERITEM
If you have two tables instead of one, then:
SELECT oi.orderid,
SUM(oi.quantity * p.price) AS grand_total,
FROM ORDERITEM oi
JOIN PRODUCT p ON p.id = oi.productid
WHERE oi.orderid = #OrderId
GROUP BY oi.orderid
Adding all the rows (orderid numbers) up together to get a total, you would simply groupby and them select the sum value. Example below:
SELECT Orderid, SUM(quanity) AS Expr1, SUM(price) AS Expr2, SUM(quanity * price) AS Total
FROM dbo.mytable
GROUP BY pid
HAVING (pid = 2)
Or this in a SQL view showing the total QTY and Price:
SELECT Orderid, SUM(quanity) AS Quanity, SUM(price) AS Price, SUM(quanity * price) AS Total
FROM dbo.mytable
GROUP BY pid
Related
Which customer id bought maximum products? (hint : get sales by multiplying product_quantity and price_per_item fields)
SELECT
customerid,
product_quantity * price_per_item as "sales",
SUM(sales)
FROM questions
GROUP BY customerid
This is how the database looks like
After adding the sales column I am not able to perform the sum operation and getting the following error:
SELECT customerid, product_quantity * price_per_item as "sales", SUM(sales) FROM questions GROUP BY customerid LIMIT 0, 1000
Error Code: 1054. Unknown column 'sales' in 'field list'
Desired output
The exact error you are seeing is due to the product expression not appearing directly inside an aggregated function. You should have taken the sum of this product directly. You might be fine just using the following LIMIT query:
SELECT customerid
FROM questions
GROUP BY customerid
ORDER BY SUM(product_quantity * price_per_item) DESC
LIMIT 1;
If there could be multiple customers tied for top sales, then use:
SELECT customerid
FROM questions
GROUP BY customerid
HAVING SUM(product_quantity * price_per_item) = (
SELECT SUM(product_quantity * price_per_item)
FROM questions
GROUP BY customerid
ORDER BY SUM(product_quantity * price_per_item) DESC
LIMIT 1
);
You cannot use an alias created in a select clause in the same select clause, because the expressions have no order. This means, that product_quantity * price_per_item as "sales" is not necessarily executed before SUM(sales), so the DBMS tells you that sales is unknown.
You don't need the alias anyway, because with one result row per customer, what amount other than the sum would you want to show?
SELECT
customerid,
SUM(product_quantity * price_per_item) AS sales
FROM questions
GROUP BY customerid
ORDER BY customerid;
This doesn't get you the top customer, though. But your query didn't either :-) This is just an explanation what's wrong in your original query.
Here are two options on how to build up on this to get the top customer(s):
Option 1 with a subquery (a CTE here):
WITH list AS
(
SELECT
customerid,
SUM(product_quantity * price_per_item) AS sales
FROM questions
GROUP BY customerid
)
SELECT *
FROM list
WHERE sales = (SELECT MAX(sales) FROM list);
Option 2 with an analytic function:
SELECT customerid, sales
FROM
(
SELECT
customerid,
SUM(product_quantity * price_per_item) AS sales,
MAX(SUM(product_quantity * price_per_item)) OVER() AS max_sales
FROM questions
GROUP BY customerid
) with_max
WHERE sales = max_sales;
Calculate each supplier's total sales quantity and get the sales person's name if the sales person supplies parts more than 1000 units in total.
Table info:
Supplier {s_num, s_name, status, city}
Spj {s_num, p_num, j_num, qty}
This is what I have:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
AND qty > 1000
GROUP BY s_name;
I think my error is in this line:
AND qty > 1000
maybe I am using the "GROUP BY" incorrectly...
No suppliers quantity is above 800, so I get a blank result.
I want to test this:
"sum of quantity for each supplier" > 1000
Use HAVING to access aggregate results after a group by clause:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
GROUP BY s_name
HAVING qty > 1000;
See this post for more info on having vs where.
Need some help with a complicated SQL query.
We have the table pricechanges with the columns id, productid, date, price
For each productid, there are a number of rows with date = a timestamp and price = the price at the time of the timestamp.
Like this:
productid, date, price
3, 17/5-2016 22:00:00, 100
3, 18/5-2016 22:00:00, 120
3, 19/5-2016 22:00:00, 140
3, 20/5-2016 22:00:00, 120
3, 21/5-2016 22:00:00, 140
I would like to get the 10 products with the biggest price decrease in the last 7 days.
Is that possible somehow?
Thanks!
PS: It's a MySQL database.
IF i get the logic and then tables right the try the following:
select d.productID from
(
select a.productid, (a.price-b.price) as priceDiff
from products a
inner join products b
on a.productID=b.productID
and Date(a.date)=Date(b.date)+7
) d
order by priceDiff
Limit 10
I think I'm missing a simple step here but I can't seem to figure it out. I've read the other threads and they talk about grouping but I can't seem to put it all together right.
I have a simple table that holds inventory transactions. In each row, there is a quantity and a price. I want to get the sum of the quantity and the sum of the each price * each quantity.
Here's my query. If I remove the grouping, I get 1 result that is multiplied by the number of rows in the table. If I add the grouping, I get the correct result multiple times. Am I missing something here? I just feel like running a query to get 20k results when they all contain the same data would be pointless.
SELECT (SUM(i.quantity) - IFNULL(SUM(s.quantity), 0)) AS quantity,
SUM(i.unitprice * i.quantity) AS totalprice
FROM 02_01_transactions t
LEFT JOIN 02_01_transactions i
ON i.type = 1
AND i.active = 1
LEFT JOIN 02_01_transactions s
ON s.type = 2
AND s.active =1
GROUP BY t.id
Not sure there is a need for the joins (you are not joining on any common value) or the type = 2 rows if you are just subtracting them out. Is there a reason the following does not work?
-- Total quantity, total price of all type 1, active transactions.
SELECT SUM(quantity) AS quantity,
SUM(unitprice * quantity) AS totalprice
FROM 02_01_transactions
WHERE type = 1
AND active = 1
Here's my guess at what you were trying to accomplish:
select
sum(quantity * case type when 1 then 1 when 2 then -1 end) as quantity,
sum(unitprice * quantity) as totalprice
from 02_01_transactions
where type in (1, 2) and active = 1
I have a simple question but I can't find an answer (please give me a link if that question is on stackoverflow forum).
Table has orderid and quantity columns.
How to select orderid, quantity, total_in_order, where total_in_order is a sum of quantity for current orderid
So the table looks the same, but with additional column.
Example:
orderid - quantity - **total_in_order**
1 - 43 - **78**
1 - 24 - **78**
1 - 11 - **78**
2 - 31 - **31**
3 - 9 - **25**
3 - 16 - **25**
So, how to modify SELECT orderid, quantity, SUM(quantity) from orders; ?
Thank you.
Use a join to a subselect. In the subselect calculate the totals for each orderid.
SELECT
orders.orderid,
orders.quantity,
totals.total_in_order
FROM orders
JOIN
(
SELECT orderid, SUM(quantity) AS total_in_order
FROM orders
GROUP BY orderid
) AS totals
ON orders.orderid = totals.orderid
First approach:
SELECT
O.orderid,
O.quantity,
( select sum( O2.quantity)
from orders O2
where O.orderid = O2.orderid ) as qty
from
orders O
Here is an example on how to do it using a subquery. It should do the job.
SELECT A.orderid
A.quantity
B.sum
FROM
orders A JOIN ( SELECT orderid, sum(quantity) as sum from orders group by orderid) as B on(A.orderid=B.orderid)