How to calculate the difference between purchase and sale quantity? - ms-access

How can I calculate the difference between the purchase and sale quantity in one query using Ms Access database?
My data, for example, looks like this:
ProductId Type Quantity
1 Purchase 24
1 Sale 1
How would I get the difference of (24-1=23) in one query?

I suppose you have the database name [DB-NAME].
and Columns and rows are something like.
[Table1]
ProductID Quantity Purchase Sale
----------- --------- --------- --------
1 1 24 1
2 100 50 10
If you want to calculate the [Purchase] - [Sale] for a specific Product id Use:
( Select (Purchase - Sale) AS MyNumber FROM[DB-Name].[Table1] WHERE (ProductID=1))
//where 1 is your product id
The result table will be
MyNumber
--------
23
if you want to calculate the totals for all [ProductID] Use:
(Select (SUM(Purchase) - Sum(Sale)) AS MyNumber FROM[DB-Name].[Table1] )
The result table will be
MyNumber
--------
63

You can self-join the table:
SELECT p.productId, (p.quanity - COALESCE(s.quantity, 0)) difference
FROM table p
LEFT JOIN table s
ON p.type = 'Purchase' AND s.type = 'Sale' AND p.productId = s.productId

Related

Select records where sum of assigned quantity in one table not matches with total quantity in another table

I've two tables 'orders' and 'assigned_orders'. When I create an order it get entered in orders table with certain quantity. I assign this orders to different vendors with some quantity. so let say in orders table I've 500 quantity in total where I'm assigning 300 quantity to vendor 'A' and 100 quantity to vendor 'B'. Now, I'm left with 100 quantity unassigned.
Now I want to get a record from orders and assigned_orders table which will show like:
Orders
id
customer_name
quantity
1
cust 1
500
2
cust 2
700
assigned_orders
id
order_id
assigned_quantity
1
1
300
2
1
100
Expected result
id
order_id
quantity
assigned_quantity
1
1
500
400
use join and aggregation
select o.id,ao.order_id,
sum(ao.assigned_quantity) as assigned_quantity,
sum(o.quantity) as quantity
from assigned_orders ao join
Orders o on o.id=ao.order_id
group by o.order_id,o.id

SQL task if the product has no sales in the given month, then display 0 in this month

I have to build and SQL query which must do these things:
select all products from table "products" - satisfied
SUM all sales and forecast to the next 3 months - satisfied
check if the product has no one sale, then write "0" -> here is the problem, because I don't know how to do that..
My SQL query is here..
select product.name,
(select sum(amount)
from forecast
where forecast.product_id = product.id),
sum(sale.amount)
from product join
 sale
  on sale.product_id = product.id
where sale.outlook > -4
group by product.id
Here is the products table:
id name
1 milk
2 roll
3 ham
Table sale (same structure like forecast):
product_id outlook amount
1 -1 9
1 -2 13
1 -3 14
2 -1 88
2 -3 61
3 -1 33
3 -4 16
You can use left join to bring in the rows and coalesce() to get the 0 instead of NULL:
select p.name,
(select sum(f.amount)
from forecast f
where v.product_id = p.id),
coalesce(sum(s.amount), 0)
from product p left join
sale s
on sale.product_id = product.id and
sale.outlook > -4
group by p.id
Understand the requirement to be, show the sales per product and if no sale for a product show "0". Tables are named Products and Sale.
For this, "with" statements are useful and help understanding too:
With SalesSummary as
(
select product_id, sum(amount) as ProductSales
from Sale
Group by product_id
)
select a.ProductID, ISNULL(b.ProductSales,0) as Sales
from products a left join SalesSummary b on a.product_id=b.product_id

Update query subtract same sale model rows from stock table?

I have two MySQL tables:
> Sale
--------------------------------------------------------
ID customer model qty
--------------------------------------------------------
1 Ali H46 1
1 aslam H46 1
1 kasif H46 1
1 umer H46 1
1 naveed H46 1
---------------------------------------------------------
> Stock
---------------------------------------------------------
ID model qty
---------------------------------------------------------
1 H46 40
1 H47 30
1 H48 20
1 H49 60
1 H50 20
---------------------------------------------------------
MySQL query I applied
UPDATE sale AS T1
INNER JOIN stock T2
ON T1.model = T2.model
SET T2.qty = (T2.qty - T1.qty)
In result in stock table only one qty update doesn't total sale qty minus from stock table.
I want all sale qty minus from stock. But my query result return 1 record update in stock.
I think you need to join to an aggregate of the Sale table:
UPDATE stock st
INNER JOIN
(
SELECT model, SUM(qty) AS qty
FROM sale
GROUP BY model
) s
ON st.model = s.model
SET
st.qty = st.qty - s.qty;
An inner join update (rather than a left join update) should be fine here, since models which had zero sales would not require updating the stock anyway.

Getting the Maximum Average price based on Product Name

I have a Products table with three headers namely ProductID, ProductName and Price.I need to find the name of the product which is having maximum average price. There are multiple products and they are duplicate as well for different product ID and i need their average price.
Sample Data looks like below :
ProductID ProductName Price
1 A 18
2 A 19
3 B 10
4 C 22
5 D 21.35
6 C 25
7 A 30
8 B 40
9 E 97
10 A 31
Query that i have written is as below:
SELECT ProductName
FROM Products
group by ProductName
order by avg(Price) desc
limit 1;
But i was informed that it is not a generic solution. Can anyone please inform, what is wrong in this query.
The only problem with your query is the case where there are more than 1 products with average price equal to the maximum average price, because it returns only 1 row.
To cover this case you need to group again and use your query in the HAVING clause :
select productname
from products
group by productname
having avg(price) = (
select avg(price)
from products
group by productname
order by avg(price) desc
limit 1
);
See the demo.

SQL Pricing Tiers

I have a pricing lookup table in MySQL where I need to lookup the right pricing based on the transaction quantity.
Say for example, i have a pricing table pricing, looks like:
product quantity price
prod1 1 4
prod1 10 3
prod1 100 2
prod1 1000 1
prod2 1 0.4
...
And I have a table called transaction where contains the sales data:
product sales
prod1 144
prod2 2
...
How can I get the sales multiply by the right unit price based on the quantity.
Something likes:
product sales quantity unitPrice
prod1 144 100 2
prod2....
I tried to join two table on product but don't know where to go from there.
One way to get the price is using a correlated subquery:
select t.*,
(select p.price
from pricing p
where p.product = t.product and p.quantity >= t.quantity
order by p.quantity
limit 1
) as price
from transaction t;
A similar subquery can be used to get other information such as the pricing tier.
For performance, you want an index on pricing(product, quantity).