profit for products - sql

I d like to create a query gives me Overall revenue per product greater than 150000 in in descending order. You can see my tables and desired output on images I have added

Whats not clear from your question is how you want to calculate revenue. Assuming that revenue per product is calculated as unit price x quantity from the Order_Items table:
SELECT product_id, SUM(unit_price * quantity) AS revenue
FROM Order_Items
GROUP BY product_id
HAVING SUM(unit_price * quantity) > 150000
ORDER BY revenue DESC

Related

How do I find out what items are less purchased in the store؟

We have two table , the first is
products (pro_id,pro_name,supleir_id,quantity,unit,price,enter_date)
the second table is
customers(cus_id,cus_name,purchased_item,pro_id,quantity,total_price,date,invoice)
We want to create a procedure using PLSQL to know which products are less purchased by customers
I think "less purchased" means the product id with the lowest occurrences. However, that is still ambiguous whether it means lowest number of times purchased (count of pro_id) or lowest number of items sold (sum of quantity) from customers.
Either way one method would to rank the target value and then select only the the rows with the lowest ranking. It creates a Subquery Factoring (more commonly known as CTE) to rank the product from the Customers table by quantity. It then Joins the result with the Product table. ( See Demo )
with less_purchased(pro_id, number_purchased) as
( select pro_id, cnt
from (-- rank each product by quantity
select pro_id, cnt, dense_rank() over( order by cnt desc) rnk
from (-- get sum of quantity for each product
select pro_id, sum(quantity) cnt
from customers
group by pro_id
) sum_query
) rank_query
-- discard all but rank 1
where rnk = 1
)
-- get product information and quantity purchased
select p.*, lp.number_purchased
from products p
join less_purchased lp on (lp.pro_id = p.pro_id);
The above may produce output with multiple product rows as products with the same quantity will have the same rank.
To get same for number of times purchased replace sum(quantity) with count(*) and remove desc or order by.

How to find the percentage of a single product of a supplier's total revenue?

There are multiple suppliers and each has sold multiple products. How would I find the percentage of the revenue of one of their products against that supplier's total sales in SQL?
Here is an example of data I have to work with:
supplier
product
price of product
quantity sold
Supplier A
Apples
$2
1
Supplier A
Shoes
$3
2
Supplier B
Shirts
$1
3
The revenue of Supplier A's apples should be $2, their total revenue is $8. So the percentage of Supplier A's apples against total sales should be 25%.
You would use divide values . . . and use window functions:
select supplier, product, sum(price * quantity),
sum(price * quantity) / sum(sum(price * quantity)) over (partition by supplier) as ratio
from t
group by supplier, product;

SQL (DB2) query to get count of top 10% revenue contributing customers

I work for a telecom company and I need to run a scheme for top valued customers who contributed 10% of total company's revenue in the month. I want to know the count of customers who are eligible for this scheme? I am using SQL DB2.
Ex - In the below table, Sum of the Revenue is 5000 and its 10% is 500, and I want to know the count of minimum number of customers whose sum of revenue would be either 500 or just above 500
Customers Revenue
A 156
B 259
C 389
D 125
E 578
F 321
To find all customers where their total revenue is at least 10 percent of the overall revenue:
select customer
from the_table
group by customer
having sum(revenue) >= (select sum(revenue) * 0.1 from the_table);
Your sample data doesn't show this, but this also deals with multiple rows per each customer in the table (your example only has a single row per customer)
The get the count of that:
select count(*)
from (
select customer
from the_table
group by customer
having sum(revenue) >= (select sum(revenue) * 0.1 from the_table)
) t
I interpret the question as wanting the highest revenue customers whose sum is at least 10% of the total revenue.
You need a cumulative sum for this:
select count(*)
from (select t.*, sum(revenue) over (order by revenue desc) as cume_rev,
sum(revenue) over () as tot_rev
from t
) t
where cume_rev <= tot_rev * 0.1;
This assumes that there is one row per customer.
EDIT:
For "just above", the where clause should be:
where cume_rev - revenue < tot_rev * 0.1;

PostgreSQL: Finding the total line total for each product that was sold in the largest order?

I'm a college student who is currently taking a Database course, using PostgreSQL. There is one question that seem's to give me some trouble.
Here are two tables:
Table1-orders
Fields-id, order_date, customer_id, credit_card_number, cvv, and order_total
Table2-order_lines
Fields-id, order_id, product_id, quantity, sell_price, and line_total
Here is the question:
What was the total line_total for each product that was sold in the largest order? (largest order_total).
Enter 0 if the product was not in the largest order. Make sure to enter both decimal places.
So far, this is the syntax I have:
select product_id,
sum (line_total * (1) * quantity)
from order_lines
group by product_id;
I was able to output the product_id with the total sum of the line_total but I was wondering how the total line_total for each product was sold in the largest order.
Should I find the id of the largest order based on the order_total? Use a sub query to merge the the two tables to get the final answer? Using the sell_price field with the syntax I have above?
First you need to get the largest order. This is done by taking the first record ordered by order_total DESC:
SELECT *
FROM orders
ORDER BY
order_total DESC
LIMIT 1
Then you need to get all products in that largest order. Just join it with order_lines and select distinct product ids:
SELECT DISTINCT
product_id
FROM (
SELECT *
FROM orders
ORDER BY
order_total DESC
LIMIT 1
) o
JOIN order_lines ol
ON ol.order_id = o.id
Finally, we need to join this resultset with your query, substituting the sums with zeros if the product was not in the largest order:
SELECT product_id,
CASE WHEN lo.product_id IS NULL THEN sum_line_total ELSE 0 END
FROM (
SELECT product_id, SUM(line_total) sum_line_total
FROM order_lines
GROUP BY
product_id
) ps
LEFT JOIN
(
SELECT DISTINCT
product_id
FROM (
SELECT *
FROM orders
ORDER BY
order_total DESC
LIMIT 1
) o
JOIN order_lines ol
ON ol.order_id = o.i
) lo
USING (product_id)
We could further optimize this query by not calculating sums for the products which are not in the largest order, but this would be enough to get you going.

Percentage of the total revenue

I have category, Sub-category and Revenue.
Category Sub-Category Revenue Percentage
---------------------------------------------------------
Books Text Books 5000 (5000/14000)*100
Comics Books 6000
Horror Books 3000
Now my question is, how to achieve like this using SQL?
I need percentage wrt to total sales for that category.
Oracle has RATIO_TO_REPORT analytic function, which computes the ratio of a value to the sum of a set of values.
select category, subcategory, revenue,
round((ratio_to_report(revenue) over (partition by category) * 100),2) as percentage
from mytable;
Output:
CATEGORY SUBCATEGORY REVENUE PERCENTAGE
-----------------------------------------
books text 5000 35.71
books horror 3000 21.43
books comics 6000 42.86
Sample fiddle here
You can use an analytical SUM function:
select
Category,
SubCategory,
Revenue,
Revenue * 100 / sum(Revenue) over (partition by Category) as Percentage
from
yourTable
;
You can use a subselect to get the overall revenue and use it in the calculations.
SELECT category, sub-category, revenue,
((revenue /
SELECT sum(inne.revenue)
FROM table inne
WHERE inne.category = oute.category) * 100) AS percentage
FROM table oute