Last price for each item and each suppliers - ms-access

I have trans table contains
Productid, price, date, supplerid
feilds
How can i get latest price for each item and each suppliers?

Related

Select rows from sales table when sale date is > than the lowest order_date in order table for each ProductID

I Have 2 tables, one for Product sales and another for Product orders from suppliers.
Sales table contains : (ProductID, Sale date, customer ID, etc)
Orders table contains (ProductID, Order date, Supplier ID etc).
Each product on sales table was sold many times and each product of Orders table was ordered many times.
For each product ID, I want to query the sales tables to return only rows where the sale dates of that product are higher than the earliest order date for that product. For example, there may be a product that was sold even before the very first time it was ordered from the suppliers, for each productID, I want to eliminate those rows with such dates and only keep those that came after the earliest order from suppliers. Any ideas on how to go about doing that in SQL, MySQL or NetezzaSQL? Thanks !
Sales Table:
ProductID | Sale_date | CustomerID
Orders Table :
ProductID | Order_Date | SupplierID
Any help or tips would be appreciated!
select s.*, o.*
from sales s
inner join (select product.id,min(order_date) min_order_date, supplier_id from orders) o
on s.product_id = o.product_id and s.sale_date > o.min_order_date
If I understood correctly the above should do the trick!
In any case dont hesitate to comment here with more questions :)
Ted

mysql query to get the revenue, cost and profit from the below tables:

I have the below tables in database, I need to calculate the revenue(sum of sell price of all orders ), the cost (sum of all costs of all sold products) and the profit (the revenue minus the cost)
sales_order
id
grand_total (sum of sell_price of its items)
order_status (pending/complete)
delivery_date (date)
sales_order_item
id
sales_order_id
product_id (unique)
quantity (ordered quantity)
sell_price (sell price for single item)
purchase_order
id
status (pending / approved)
purchase_order_items
id
purchase_order_id
product_id
cost (cost of each item)
Joining on your sales_order_item table to a distinct pull of cost and product from your purchase_order_items table should work assuming that cost isn't different between purchase_orders over time. If cost has changed then this could get a little ugly.
SELECT sum(sell_price*quantity) as revenue, sum(cost*quantity) as cost, sum(sell_price)-sum(cost*quantity) as profit
FROM sales_order_item
INNER JOIN (SELECT distinct cost, product_id FROM purchase_order_items) as product
ON sales_order_item.product_id = product.product_id

Joining two tables by multiple fields in MS Access

I have two tables. One lists my store's Customer Number, The Item They Bought, The Date They Bought It, and the Price They Paid.
I have a second table that lists my store's Customer Number, The Item They Returned, The Date They Returned It, and the Credit They Received.
Obviously, not all Sales have Credits.
I want to return results that look like:
Customer1, Item1, Date, Price, Blank
Customer1, Item2, Date, Price, Credit
Customer1, Item3, Date, Price, Blank
Customer1, Item4, Date, Price, Blank
Customer2, Item1, Date, Price, Blank
Customer2, Item2, Date, Price, Blank
Customer2, Item3, Date, Price, Credit
etc.
If I join the tables on the Customer Number, it returns me:
Customer1, Item1, Date, Price, Credit
Customer1, Item2, Date, Price, Credit
Customer1, Item3, Date, Price, Credit
Customer1, Item4, Date, Price, Credit
Customer2, Item1, Date, Price, Credit
Customer2, Item2, Date, Price, Credit
Customer2, Item3, Date, Price, Credit
If I join the tables on the Item and sort by Customer number, it returns gobbledygook.
My idea was to join on the Customer number and use an IIf(Sales.Item1=Credits.Item1, Credit, 0) in the criteria, but I am getting an error "You tried to execute a query ... that is not part of an aggregate expression."
What am I doing wrong?
Assuming you will have no returns without Sales, try this Query:
SELECT Sales.Customer, Sales.Item, Sales.Sale_Date, Sales.Sale_Price,
Credits.Credit_Date, Credits.Credit_Price,
IIf(IsDate([Credits].[Credit_Date]),"Credit","Blank") AS Result
FROM Sales LEFT JOIN Credits ON (Sales.Customer = Credits.Customer)
AND (Sales.Item = Credits.Item);
Define your values in the IIF statement (Credit or Blank) as needed.
You also can hide your fields Credits.Credit_Date, Credits.Credit_Price, so the output will be exactly what desired.

SQL select query, is this a correct solution?

I have been practicing SQL online and I am having doubt in one exercise. In the following database:
product (prod id, product name, product price, product manufacturer)
customer (cust id, customer name, customer address)
rating (prod id, cust id, rating date, rating stars)
sale (sale id, prod id, cust id, sale quantity, sale cost)
"For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given."
I am not entirely sure about the answer, this is what I got:
SELECT customer_name,
product_name,
min(rating_stars)
FROM customer,
product,
rating
WHERE rating_stars < SOME
(SELECT rating_stars
FROM customer,
product,
rating)
GROUP BY customer,
product,
rating
Could any of you have a check in the code and see if it is correct?

compare AVG(integer) with AVG(integer) of a category

i have relation with three columns: ProductName, CategoryID and Price. i need to SELECT only those products which Price is higher than average product Price in given category.
(eg when apple(ProductName) is a fruit(CategoryID) it should be selected because its price is higher than average fruit price). How do i do that?
I'm thinking of something like this, but its obviously wrong since I'm comparing Price with Price and CategoryID:
SELECT ProductName, AVG(Price) FROM `products`
WHERE (SELECT AVG(Price)) > (SELECT CategoryID, AVG(Price))
GROUP BY ProductName;
Something like this
SELECT ProductName, Price, CategoryID FROM `products` p
WHERE Price > (SELECT AVG(Price) from `products` where CategoryID = p.CategoryID)