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

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

Related

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

replenishment formula oracle sql query

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)

Merging two SQL tables

I have a table called Store Sales with the following columns
Date
Total Qty Sold
RRP
Total Value Sold
Branch No.
Barcodes
Unit Cost
Then I have another table called ESales that contains this
Inv Date
Our Ship Qty
Unit Price (RRP Inc VAT)
Line Total
Invoice
Order
Line
Brand
Part
Description
Our Order Qty
Unit Price (Exc VAT)
Discount %
Discount Amt (Inc VAT)
Discount Amount (Exc VAT)
Tax Category
Tax Exempt
Group
Sales Cat
Cust. ID
Title
Customer
Name
Tax ID
Rep. ID
Credit Memo
Unit Price
Amount
Category ID
Cust. Amount
Number01
ShortChar01
ShortChar02
Clubcard
There are matching fields but none with the same name. They are
Inv Date = Date
Our Ship Qty = Total Qty Sold
Unit Price (RRP Inc VAT) = RRP
Line Total = Total Value Sold
What I want to do in merge the values in StoreSales to ESales and create additional columns for the data that is not there, these are
Branch No
Barcodes
Unit Cost
Any ideas how to insert the matching values and create the three new ones?
Instead of creating a new table, I would just start with a query that shows you all the data you are describing. The way you merge data from multiple tables together is by using a JOIN command. Here is a example query that shows everything from the ESales table and the additional columns from the StoreSales table you described:
SELECT e.*, s.[Branch No], s.[Barcodes], s.[Unit Cost]
FROM StoreSales s
INNER JOIN ESales e ON
e.[Inv Date] = s.[Date] AND
e.[Our Ship Qty] = s.[Total Qty Sold] AND
e.[Unit Price (RRP Inc VAT)] = s.[RRP] AND
e.[Line Total] = s.[Total Value Sold]
Once you get a query you like, you can save it as a VIEW which essentially lets you interact with the result of this query as if it were a separate table.
I will caution you that you have to be very careful doing this. Usually, data will have some kind of common field like a Order ID or a product SKU that makes it very clear what unique item you are referencing in the database. Joining by things like date, order total, price, etc is bad practice. This is because there is no way to guarantee that there aren't two orders with the same date or the same order total. Those things are not unique to any one particular order.

How to get total number of products in virtuemart?

I want to display total number of products sold for each product. There are around 30 products in my virtue mart and I want each one's total sold number.
Table named jos_vm_order_item contains product_id and product_quantity with all other information.
How can I display this?
SELECT O.product_id,
SUM(O.product_quantity) as Total_Sold
FROM jos_vm_order_item O
GROUP BY O.product_id