Calculate value from two field in third field - ms-access

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

Related

Retrieve columns from three tables

I want to use columns from three different tables and use them to calculate how much each customer has ever spent.
tblCustomer(CustomerID)
tblOrder(CustomerID, ProductID, Amount)
tblProduct(ProductID, Price)
So I want to filter out the orders made by a customer, check what product they ordered and what amount of it, check the price and multiply it with the amount they ordered and do that with every customer
expected result:
CustomerID | TotalSpent
1|20
2|130
This depends on the relationship between the tables. The below assumes there is only one price per product in the price table. The customer table is excluded as it is unnecessary but could be joined again using the customerID
SELECT tblOrder.CustomerID,
SUM(tblOrder.Amount*tblProduct.Price) AS 'TotalSpent'
FROM tblOrder JOIN tblProduct ON tblProduct.ProductID = tblOrder.ProductID
GROUP BY tblOrder.CustomerID

Got orders and order_products tables.Should total price of order be stored in the orders table, or calculated based on the products quantity and price

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

Get correct details when using MAX() function in mySQL

So I have two mySQL tables: products and prices. In products I have the informations about the product (id, name, type, date,...) and prices include informations for the prices for a products. i.E. product USB with ID CF003X from table products is available in tableprices with sellers (A, B, C and prices 18.00, 12.00, 13.50).
Now, when I want to select the highest price for a date, groupped by product IDs, the query returns the highest prices, but doesnt show correctly from which seller.
I also want to get results where i.E. seller 'Asus' had the highest price1
SQL Fiddle
This is a typical example where subqueries can be used: create a subquery that returns the highest price for product in a certain data range, then join this back to the prices table and get the supplier(s) who supply at that price. The date range, however, should probably be in the prices table, not in the products one. Prices change, products not really (they can be flagged as not to be listed). You should also consider having a product and a pricing id in the pricing table.
However, I'm going along with the fields you supplied within the sqlfiddle example.
select prod.id, p.seller, p.price1, p.price2
from prices p
inner join (select id, max(price1) as maxprice from prices group by id) mp on p.id=mp.id and p.price1=mp.maxprice
inner join products prod on p.id=prod.id
where prod.date='...'
You can extend the above query to show a specific vendor only by adding that vendor to the where criteria.

sum of products in stock

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

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.