sum of products in stock - mysql

i need to show the total valvue of all the all-in-one printers in stock from the products table
the all-in-one printers can be identified by having 'A1' in positions one and two of the product id
here is what ive tried so far
select sum(price) AS total_price
from products
where prod_id like 'A1%'
here is a picture of the products table

so if i understand you correctly you want the price per item times the number on hand all summed up if thats the case this should do the trick
SELECT SUM(adjusted_price)
FROM
( SELECT price * on_hand adjusted_price
FROM products
WHERE prod_id like 'A1%'
)t
DEMO

Related

Get Min Price From Product Variation Table in MySQL

Product Table
Variation Table
How do I get a result from Product with every product's min price from Variation?
I join by description, which should work if no different products have the same description. It would be better to join by something more specific like product_id, but product_table does not have that as a column.
SELECT
p.*,
v.min(price) as min_price,
FROM
variation_table v
JOIN
product_table p ON v.description = p.description;
You could probably add ORDER BY product_id to do in order of the product ids or whatever other field you'd prefer.

Multiple the amount of sold products with the price of the product

My tables look like this:
tblartikel(artikelid primary key, price)
tblbestallning(artikelid, amount)
I saw someone do this:
select artikelid, sum(amount) as AmountSold from tblbestallning group by artikelID
this correctly showed the artikelid's and thee amount it has ever been bought
how do i multiply the times it has been bought with the price?
This depends on the relationship between the two tables but assuming there is only one price per artikel, this would be a good starting point
SELECT tblbestallning.artikelid,
SUM(amount) as AmountSold,
SUM(amount*price) AS 'TotalSold'
FROM tblbestallning
JOIN tblartikel ON tblartikel.artikelid = tblbestallning.artikelid
GROUP by tblbestallning.artikelid

mysql adding total price of certain products

hi i'm having problems querying a products table at the moment:
i need to display the total cost of all HP and toshiba products..
this is what i have tried so far
SELECT * FROM products
WHERE prod_id LIKE '__hp%'
AND SELECT SUM(price) AS total FROM products;
any help would be appreciated
this is a pic of the products table -->>
Thank you;
You could help yourself a lot if it is clear that the 3rd and 4th characters of Prod_ID are a manufacturer code. HP and TA for Toshiba.
SELECT SUBSTRING(prod_id,3,2)
,SUM(price * on_hand)
WHERE SUBSTRING(prod_id,2,1) IN ('TA','HP')
GROUP BY SUBSTRING(prod_id,3,2)
Seems like you're looking for something like this:
SELECT SUM(price) AS total_price
FROM products
WHERE prod_name LIKE 'HP%' or prod_name LIKE 'Toshiba%';

Joining 3 tables on MySQL to sum sales and stock for a product

I have 3 tables:
products (sku, price, priceOffer, etc)
stock (sku, branch, items)
sales_provider (sku, items, date)
The table products holds all the information about a product, except for stock or sales. Current stock is stored in the table stock, and has information about how many items are available on each branch. The table "sales_provider" stores how many items were sold each day for every product. The product ID is "sku".
Now, I'm trying to get with one query the product that:
Has generated the best profit (number of sales * offered price)
Is still available on stock
And, of course, I want to know how many items are still on stock and how many items were sold.
I'm trying a query like this:
select
*
from
(
select
p.*,
sum(s.items) stock,
sum(sp.items) sales,
case when
p.priceOffer < p.price
and
p.priceOffer > 0
then
p.priceOffer
else
p.price
end finalPrice
from
products p
join
stock s
on
s.sku = p.sku
join
sales_provider sp
on
sp.sku = p.sku
group by
sku
) temp
where
stock > 0
order by
(finalPrice * sales) desc
limit 1;
But I'm having problems with that. Basically, I'm getting a huge sum of stock items ans sales_provider items, not the real amounts. Also, it's a slow query (it's taking about half a second with only 9,500 products).
I've been trying to modify it and I'm having doubts about the subquery being necessary, but I just can't nail it.
If someone can help me improve it and get the correct result, I'll really appreciate it.
Thanks in advance for any helpful comment.
Francisco
For this type of query, you want to do the aggregations separately on stock and sales_provider. Otherwise, you will generate a cartesian product between the two tables for a given item.
Try this:
select p.sku, (salesitems*offeredprice) as profit, stockitems, salesitems
from products p left join
(select sku, SUM(items) as stockitems
from stock
group by sku
) s
on p.sku = s.sku left join
(select sku, SUM(items) as salesitems
from sales_provider sp
group by sku
) sp
on p.sku = sp.sku
where p.stockitems > 0
order by profit desc
This assumes that product(sku) is unique.

Calculate value from two field in third field

I'm trying to create a query, that will calculate sum of products on invoice. I have 3 tables :
Product (with product's price)
Invoice (with invoice id)
Products on invoice (with invoice id, product id and number of particular products)
So in my query I take invoice_id (from invoice), price (from product),number of products sold and invoice_id (from products on invoice) and calculate their product in fourth column. I know I should use 'Totals' but how to achieve that ?
The following added to your SELECT should do it.
[Product].[price] * [Products on invoice].[number of products on invoice] AS Total
If you include the fields and table names, I can give you a much more accurate statement.
Edit:
SELECT
invoice.invoice_id,
product.price,
products_on_invoice.amount,
product.price * products_on_invoice.amount AS Total
FROM
invoice INNER JOIN
(products_on_invoice INNER JOIN
product
ON products_on_invoice.product_id = product.product_id)
ON invoice.invoice_id = products_on_invoice.invoice_id