Find the top seller - sql

I am trying to find the top seller here. I was trying to use the below but i dont think this is right. I can see other ids that have bigger totals plus i think i should be using a SUM function instead of MAX.
select selleruserid
from sales_fact
where price = (select max(price) from sales_fact)

As I understand it you want to get the total sales of each seller and choose the seller who has the most sales.
The seller who has the most sales:
SELECT max(totalPrice) as totalPrice,selleruserid
FROM (SELECT sum(price) AS totalPrice,selleruserid FROM sales_fact GROUP BY selleruserid)
Total sales of each seller:
SELECT totalPrice as totalPrice,selleruserid
FROM (SELECT sum(price) AS totalPrice,selleruserid FROM sales_fact GROUP BY selleruserid)

You can use FETCH clause with TIES option as follows:
select selleruserid, sum(price) as total
from sales_fact
group by selleruserid
order by sum(price) desc
FETCH FIRST 1 ROW WITH TIES
OR You can use the analytical function RANK as follows:
select selleruserid, total from
(select selleruserid, sum(price) as total,
RANK() OVER (ORDER BY sum(price) DESC) AS rn
from sales_fact
group by selleruserid) t
where rn = 1

Related

Which store type sells the maximum products by the value of sales amount & by quantity sold

I have to use only single SELECT statement. Multiple select statements are not allowed.
I have a table named "Transactions" which has the following variables:
Qty - It has total Quantities ordered for a particular product
total_amt - It has Amount paid by customers for the particular products
Store_type - It has transaction channels
For example, if Qty ordered are 5 and price of each quantity of product is 100. then total_amt would be carrying 500.
Now I have to find the Store type that sells the maximum products by the value of sales amount & by quantity sold. I have to do this using sub queries in Having clause.
I have written the following code in SQL Server but it is not giving me any output and throwing the error that Transactions.total_amt should be included in having clause or aggregated function. On using total_amt in group by, I am not getting any result in the output.
Below is the query which I have written :
select store_type, MAX(total_amt), MAX(Qty)
from Transactions
group by Store_type
Having total_amt = (select SUM(total_amt) from Transactions group by Store_type)
AND
Qty = (select SUM(Qty) from Transactions group by Store_type)
A simple way is based on max order by and limit
select store_type, MAX(total_amt), MAX(Qty)
from Transactions
group by Store_type
ORDER BY MAX(total_amt) DESC, MAX(Qty) DESC
LIMIT 1
Check both query result and identify store type wise total amt and max total amt matching with qty or not.
If you want only matched then you can create 2 sub query tables and use inner join.
I hope this will resolve.
select
Store_type,
SUM(total_amt) AS Total_total_amt,
SUM(Qty) AS Total_Qty
from Transactions
group by Store_type
select
store_type,
MAX(total_amt) AS max_total_amt,
MAX(Qty) AS max_Qty
from Transactions
group by Store_type
You can use a query as shown below
;With cte as
(
Select sum(qty) as sum_quantity,sum(sales) as sum_sales,store_type
From transaction
Group by store_type
)
Select Max(sum_quantity),max(sum_sales),store_type from cte
Group by store_type
Select store_type
From transactions
Where total_amt > 0 and qty>0
Group by store_type
Order by sum(total_amt) desc,sum(qty) desc ;

SQL: How to select the highest priced used item for each day

I need to produce a query that would give me the highest priced used product for each day where the total price of products sold that day exceeds 200.
SELECT *, max(price)
FROM products
WHERE products.`condition` = 'used' and products.price > 200
GROUP BY date_sold
Here is my products table http://prntscr.com/of3hjd
You could try using a join with sum for price > 200 group by date_sol
select m.date_sold, max(m.price)
from my_table m
inner join (
select date_sold, sum(price)
from my_table
group by date_sold
having sum(price)>200
) t on t.date_sold = m.date_sold
group by m.date_sold
You can use window functions for this:
select p.*
from (select p.*,
sum(price) over (partition by date_sold) as sum_price,
row_number() over (partition by date_sold, condition order by price desc) as seqnum
from products p
) p
where sum_price > 200 and
condition = 'used' and
seqnum = 1;
SELECT *, max(price) FROM products
where products.`condition` = 'used' and sum(products.price) > 200
GROUP BY day(date_sold)

SQL query for table with multiple keys?

I am sorry if this seems too easy but I was asked this question and I couldn't answer even after preparing SQL thoroughly :(. Can someone answer this?
There's a table - Seller id, product id, warehouse id, quantity of products at each warehouse for each product as per each seller.
We have to list the Product Ids with Seller Id who has highest number of products for that product and the total number of units he has for that product.
I think I got confused because there were 3 keys in the table.
It's not quite clear which DBMS you are using currently. The below should work if your DBMS support window functions.
You can find count of rows for each product and seller, rank each seller within each product using window function rank and then use filter to get only top ranked sellers in each product along with count of units.
select
product_id,
seller_id,
no_of_products
from (
select
product_id,
seller_id,
count(*) no_of_products,
rank() over (partition by product_id order by count(*) desc) rnk
from your_table
group by
product_id,
seller_id
) t where rnk = 1;
If window functions are not supported, you can use correlated query to achieve the same effect:
select
product_id,
seller_id,
count(*) no_of_products
from your_table a
group by
product_id,
seller_id
having count(*) = (
select max(cnt)
from (
select count(*) cnt
from your_table b
where b.product_id = a.product_id
group by seller_id
) t
);
Don't know why having id columns would mess you up... group by the right columns, sum up the totals and just return the first row:
select *
from (
select sellerid, productid, sum(quantity) as total_sold
from theres_a_table
group by sellerid, productid
) x
order by total_sold desc
fetch first 1 row only
If I do not think about optimization, straight forward answer is like this
select *
from
(
select seller_id, product_id, sum(product_qty) as seller_prod_qty
from your_table
group by seller_id, product_id
) spqo
inner join
(
select product_id, max(seller_prod_qty) as max_prod_qty
from
(
select seller_id, product_id, sum(product_qty) as seller_prod_qty
from your_table
group by seller_id, product_id
) spqi
group by product_id
) pmaxq
on spqo.product_id = pmaxq.product_id
and spqo.seller_prod_qty = pmaxq.max_prod_qty
both spqi (inner) and sqpo (outer) give you seller, product, sum of quantity across warehouses. pmaxq gives you max of each product again across warehouses, and then final inner join picks up sum of quantities if seller has highest (max) of the product (could be multiple sellers with the same quantity). I think this is the answer you are looking for. However, I'm sure query can be improved, since what I'm posting is the "conceptual" one :)

Using max function without grouping

I have three sets of information:
productID
date
seller
I need to get information of last product sold for each productID and sold by. I tried using max value of date but that forces me to use grouping for seller as well but I don't want to group by seller. I want to group by productID, get the date it was sold last and by who. How can I avoid grouping on seller?
Use Window function which will help you to find the Latest date in each group(productId)
SELECT ProductID,
[date],
seller
FROM (SELECT Row_number()
OVER(
partition BY ProductID
ORDER BY [date] desc) Rn,
*
FROM tablename) a
WHERE rn = 1
or use can also use Max aggregate with group by to get the result
SELECT ProductID,
[date],
seller
FROM tablename a
JOIN (SELECT Max([date]) [date],
productid
FROM tablename
group by productid) b
ON a.productid = b.productid
AND a.[date] = b.[date]

SQL Query: SELECT MAX SUM quantity

How do i combine a SUM and MAX in a single query?
Lets say i have a orderrule:
ProductID \ Quantity
I Could say:
Select ProductID,SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
ORDER BY SUM(Quantity) Desc
However that would return all sales, and not just the most sold product (with quantity).
Try this
SELECT TOP(1)
*
FROM
(
Select
ProductID,
MAX(Quantity) As MaxQuantity,
SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
)AS X
ORDER BY Sold DESC
So there are two ways to do it - first to have a limit on the number of results, something likes:
select * from (your_select) where rownum = 1
the other one is to pick the one with the the highest value, which will require a subselect, something like:
having sum(quantity) =
(select max(sum_quan) from (select sum(Quantity) from orderrule group by Product_id))
SELECT TOP 1 ProductID, Sold FROM
(
SELECT ProductID, SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
) totals
ORDER BY Sold DESC