replenishment formula oracle sql query - sql

I have a table with the following structure:
Item Code
item Description
Minimum Quantity
Maximum Quantity
Reorder Quantity
Current Stock in Location
Current Stock in Main Location
Replenishment Quantity
I would like to calculate the replenishment quantity, what would be the correct formula in oracle SQL?
Example:
Item Code - ABCD
item Description - ABCD whole item
Minimum Quantity - 20
Maximum Quantity - 100
Reorder Quantity - 20
Current Stock in Location - 15
Current Stock in Main Location - 5930
from the above, I have to calculate replenishment quantity based on the data that the replenishment quantity will be in increments of reorder quantity but should not exceed maximum quantity and only to be replenished if current stock in location is below minimum quantity and if stock is available in main location.
From the above example, I have to get replenishment quantity as 80.
Thanks in advance.

That's not really an Oracle-Question, but I guess you mean something like that, (insert your table and column-names):
select trunc((ma-cu)/re)*re
from(
select 20 mi, 100 ma, 20 re, 15 cu
from dual)
Edit:
Maybe that is also important(I am really wild guessing here):
select least(trunc((ma-cu)/re)*re, trunc(cu_m/re)*re)
from(
select 20 mi, 100 ma, 20 re, 15 cu, 5930 cu_m
from dual)

Related

SQL - how to show SKUs with the lowest price from a query

I am making a query for a price comparison site and I have to provide them a csv file with certain data. I have done the query and the first problem was that the prices that showed up in it were the full prices from sql database. So when we have for example a discount applied to the whole category of products the price shown is still the full price not the discounted one. I solved that by checking if an SKU is in a discounted category and the query calculates the price accordingly.
The new problem is now that a query checks through categories and if one sku is in multiple categories it creates a row with same SKU for each category and I end up with multiple rows with same SKU and only one has correct price (discounted one).
Example query result. NOTE that the Price column is a calculated result not the data from the table.
SKU
Product
Price
A
Ball
19
A
Ball
19
A
Ball
15
B
Cube
10
B
Cube
8
How do I filter it to just this
SKU
Product
Price
A
Ball
15
B
Cube
8
You need GROUP BY with aggregate function MIN()
SELECT SKU
, Product
, MIN(Price) as Price
FROM <table>
GROUP
BY SKU
, Product

Find out the percentage split grouped by currency

I have a result set as follows
Metal. Currency. Quantity
Gold. USD. 10
Gold. GBP. 3
Gold. AUD. 26
What I want to return is which currency had the highest percentage and by how much, so I want to say, as an example:
"85% of Gold was bought with AUD"
So essentially I am totalling up the amounts, 10,3 and 26 to get 39 the code should be along the lines of
sum(quantity) / (select total (in this case 39))
Im having trouble in returning the percentage for each currency, is there a way to do this in SQL?
You can use window funtions:
select metal, currency, sum(quantity),
sum(quantity) * sum(sum(quantity)) over (partition by metal)
from t
group by metal, currency;

Sum of 2 level discount

I am using SQL Server 2008 R2. I need to calculate sum 2 of levels discount. 1st one is item level, like 10% discount on any item. After offering this discount there is amount to receive is for example Rs.510. But shop keeper will get 500 and give him another discount Rs.10.
Now the second level discount is in main table and item wise discount is in child table. How I can calculate the sum of discount daily or monthly etc. and how I will get the following output. As when I am using inner join the discount in main table will be repeat for N number of items. Sum of discount on item wise is not an issue, but the invoice level is. Please help.
--------------------------------------------------------------
Invoice No. Date Total Discount Invoice Amount
--------------------------------------------------------------
INV-00001 01/01/2020 12 500
INV-00002 01/01/2020 10000
INV-00003 01/01/2020 30 900
--------------------------------------------------------------

How to show results as a percentage with SQL?

I am trying to complete some exercises where I must show how many products cost above 20 as a percentage of the total. I can easily show the products that cost above 20 but I am not sure how to answer the question as a percentage. The total amount of products is 77.
Display a table with (1) categoryname and (2) how many products in this category cost above 20 out of the total products in the category (in percentage).
I have written so far:
SELECT categoryname, price
FROM [Products]
WHERE price > 20
which tells me there are 37 records.
Count (and similar aggregate functions) ignore nulls. You could use a case expression to nullify products that don't cost over 20 and count it, leaving you with a count of the products that cost more than 20. Dividing this count by the total count will give you a percentage:
SELECT category, 100 * COUNT(CASE WHEN price > 20 THEN 1 END) / COUNT(*) AS percentage
FROM products
GROUP BY category

Query Help to determine average price for products < 3

Hi I've been working on this query, and I can't quite figure out the where statement to show only the average price of products < 3
"List each Product Class, number of items in the class and the average Price (name this Avg_Price, Format $0.00) of the items in the class. List only those classes that have less than three (3) items"
This query shows me the results of the average prices of the products (results in image)
COLUMN Avg_Price FORMAT $999,999.00
SELECT Product_Class, COUNT(*), AVG(Price) AS Avg_Price
FROM Product
GROUP BY Product_Class;
I need help only showing the counts avg PRICE that is < 3
Include Having at the end
HAVING count(*) > 3