리트코드 : 1211. Queries Quality and Percentage
문제
Table: Queries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| query_name | varchar |
| result | varchar |
| position | int |
| rating | int |
+-------------+---------+
This table may have duplicate rows.
This table contains information collected from some queries on a database.
The position column has a value from 1 to 500.
The rating column has a value from 1 to 5. Query with rating less than 3 is a poor query.
We define query as:quality
The average of the ratio between query rating and its position.
We also define as:poor query percentage
The percentage of all queries with rating less than 3.
Write a solution to find each , the and .query_namequalitypoor_query_percentage
Both and should be rounded to 2 decimal places.qualitypoor_query_percentage
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Queries table:
+------------+-------------------+----------+--------+
| query_name | result | position | rating |
+------------+-------------------+----------+--------+
| Dog | Golden Retriever | 1 | 5 |
| Dog | German Shepherd | 2 | 5 |
| Dog | Mule | 200 | 1 |
| Cat | Shirazi | 5 | 2 |
| Cat | Siamese | 3 | 3 |
| Cat | Sphynx | 7 | 4 |
+------------+-------------------+----------+--------+
Output:
+------------+---------+-----------------------+
| query_name | quality | poor_query_percentage |
+------------+---------+-----------------------+
| Dog | 2.50 | 33.33 |
| Cat | 0.66 | 33.33 |
+------------+---------+-----------------------+
Explanation:
Dog queries quality is ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
Dog queries poor_ query_percentage is (1 / 3) * 100 = 33.33
Cat queries quality equals ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
문제 풀이
MySQL
select query_name,
round(avg(rating/position),2) as quality,
round(100*sum(rating<3)/COUNT(*),2) AS poor_query_percentage
from queries
where query_name is not null
group by query_name
- case whem 대신 그냥 조건식을 쓰면 불리언으로 반환돼서 깔끔하게 쓸 수 있다.
- 문제에선 query name이 null인 경우는 원하지 않는다. (테케 12? 13?)
Pandas
import pandas as pd
def queries_stats(queries: pd.DataFrame) -> pd.DataFrame:
queries['r/q'] = queries['rating'] / queries['position'] + 1e-10
queries['poor'] = queries['rating'] < 3
answer = queries.groupby('query_name').agg(
quality = ('r/q', lambda x: round(x.mean(),2)),
poor_query_percentage = ('poor', lambda x: round(100*x.mean(),2))
).reset_index()
return answer
- 비슷한 방식으로 풀이.
- agg 내에서 지정되지 않은 함수를 사용할 때 lambda로 접근
- lambda 내에서도 너무 복잡하게 나오지 않도록 미리 r/q, poor을 구했다.
- round에서 오차가 발생하네용 -> 1e-10 추가
코멘트
- 조건식에 case when 안넣고 그냥 하니까 더 깔끔하고 좋은듯.
- 다중 조건으로 많이 적는 경우에는 case when이 낫겠지만, T/F식으로 나뉘는 경우는 이렇게 푸는게 좋은 듯
댓글