difference between having column_A in & having column_A = in Mysql - mysql

The question comes from leetcode sql question sales analysis I (https://leetcode.com/problems/sales-analysis-i/).
The question asks for the best seller, output expected to be:
| seller_id |
| 1 |
| 3 |
table schema:
Sales table:
seller_id
product_id
buyer_id
sale_date
quantity
price
1
1
1
2019-01-21
2
2000
1
2
2
2019-02-17
1
800
2
2
3
2019-06-02
1
800
3
3
4
2019-05-13
2
2800
I used two ways:
way 1:
SELECT seller_id
FROM sales
GROUP BY seller_id
HAVING Sum(price) IN (SELECT Max(total_sales) AS top_sales
FROM (SELECT seller_id,
Sum(price) AS total_sales
FROM sales
GROUP BY seller_id)T)
output is 1,2,3
way 2:
SELECT seller_id
FROM sales
GROUP BY seller_id
HAVING Sum(price) = (SELECT Max(total_sales) AS top_sales
FROM (SELECT seller_id,
Sum(price) AS total_sales
FROM sales
GROUP BY seller_id)T)
output is 1,3
The only differences between two queries is first one use 'IN', second one use '=', but the output is different. What's wrong with the first one?

These are the same. IN works where there are multiple rows in the subquery. However, the MAX() subquery can only return one row, because it is an aggregation query with no GROUP BY.
This construct can be written without nested subqueries:
HAVING Sum(price) = (SELECT Sum(price) AS total_sales
FROM sales
GROUP BY seller_id
ORDER BY total_sales DESC
LIMIT 1
)
However, nowadays, it is better to learn how to do this with window functions.

Related

How to find which number of orders for a customer

Given a table orders
id
customer_id
created_at
1
1
2022-09-01
2
2
2022-09-02
3
1
2022-09-03
4
1
2022-09-04
5
2
2022-09-04
How do I produce a column that describes which number in the series for the customers the order is?
Example
id
customer_id
created_at
order number
1
1
2022-09-01
1
2
2
2022-09-02
1
3
1
2022-09-03
2
4
1
2022-09-04
3
5
2
2022-09-5
2
You can use a window function for that. With a cumulative count over a partition by customer id, you get exactly the order number you need:
select orders.*,
count(*) over (partition by customer_id order by id) order_number
from orders
order by id;
In MySQL 5.7 you could do this:
select customer_id,
(select count(*)
from orders
where customer_id = main.customer_id and id <= main.id)
from orders main;

How to select sum of column values using from and to time values? [duplicate]

I have a table with the following structure and sample data:
STORE_ID | INS_TIME | TOTAL_AMOUNT
2 07:46:01 20
3 19:20:05 100
4 12:40:21 87
5 09:05:08 5
6 11:30:00 12
6 14:22:07 100
I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID.
I tried the following query but i don't know if it's correct.
SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302
WHERE MINUTE(INS_TIME) <=59
GROUP BY HOUR,STORE_ID
ORDER BY INS_TIME;
Not sure why you are not considering different days here. You could get the hourly sum using Datepart() function as below in Sql-Server:
DEMO
SELECT STORE_ID, SUM(TOTAL_AMOUNT) HOURLY_SUM
FROM t1
GROUP BY STORE_ID, datepart(hour,convert(datetime,INS_TIME))
ORDER BY STORE_ID
SELECT STORE_ID,
HOUR(INS_TIME) as HOUR_OF_TIME,
SUM(TOTAL_AMOUNT) as AMOUNT_SUM
FROM VENDAS201302
GROUP BY STORE_ID, HOUR_OF_TIME
ORDER BY INS_TIME;

How do I return the most common column value for each value in another column using mySQL?

I am looking for a tricky MySQL query to return the most common column value for each value in another column. I could use PHP to do it by each result, but it'd be so much cooler to do it with a single query :)
For example, if have a table like this, called "transactions":
Id payee exp category
1 Amazon 25.00 Gifts
2 Amazon 30.21 Books
3 Amazon 12.98 Gifts
4 Amazon 15.00 Groceries
5 Amazon 14.54 Gifts
6 Alibaba 55.55 Stock
7 Alibaba 99.00 Stock
8 Alibaba 12.00 Fun
…
The type of result I would want is like this:
payee count(payee) category
Amazon 3 Gifts
Alibaba 2 Stock
…
I can do this:
SELECT `payee`, COUNT(`payee`), `category` FROM `transactions` WHERE 1 GROUP BY category ORDER BY COUNT(`payee`) DESC
and get close to what I want:
Amazon 3 Gifts
Alibaba 2 Stock
Amazon 1 Books
Amazon 1 Groceries
Alibaba 1 Fun
but I don't want the non-maximum counts (like Amazon,1,Books for example).
Do I have to do a subquery or something? Or use in?
You could filter the results of your existing query with a correlated subquery in a having clause, as follows:
select payee, count(*), category
from transactions t
group by payee, category
having count(*) = (
select count(*)
from transactions t1
where t1.payee = t.payee
group by category
order by count(*) desc limit 1
)
order by count(*) desc
Demo on DB Fiddle:
payee | count(*) | category
:------ | -------: | :-------
Amazon | 3 | Gifts
Alibaba | 2 | Stock
Alernatively, if you are running MySQL 8.0, you can rank the categories of each payee with window function rank() over(), and filter on the top record per group:
select payee, cnt, category
from (
select
payee,
count(*) cnt,
category,
rank() over(partition by payee order by count(*) desc) rn
from transactions
group by category, payee
) t
where rn = 1
Demo on DB Fiddle

how to get total order price using mysql?

there are two tables one is order and 2nd one is order_details respectively,
order table
order_id order_name
1 shoes
2 wallet
3 socks
4 bats
order_details table
order_details_no order_id(foregin key) order_price
1 1 25
2 1 55
3 2 65
4 4 30
5 4 60
My question is, I want result set which includes order_id, order total price in ascending order (eg order 1 total is 80,order 4 total is 90 )
How to get this ?
select order_id,
sum(order_price) as total_sum
from order_details
group by order_id
order by total_sum asc
Select
order.order_name,
sum(order_details.order_price) as price
from order
join order_details
on order_details.order_id=order.order_id
group by
order.order_id
order by
price desc

Count up different products

I have the following table:
order_id product_id
1 102
2 105
3 102
4 96
5 96
How would I get a count of the product_ids. The result I am looking to attain is:
product_id count
96 2
102 2
105 1
The query I have tried is:
SELECT product_id, sum(product_id) from orders
But this of course produces an incorrect result with only one row. What would be the correct query here?
SELECT product_id, COUNT(*) FROM orders GROUP BY product_id
Or if you want to name it...
SELECT product_id, COUNT(*) as product_count FROM orders GROUP BY product_id
try this
SELECT product_id, count(*) as count from orders GROUP BY product_id