[1211] Queries Quality and Percentage

2022. 7. 1. 02:34·SQL/leetcode

able: Queries

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| query_name  | varchar |
| result      | varchar |
| position    | int     |
| rating      | int     |
+-------------+---------+
There is no primary key for this table, it 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 quality as:

The average of the ratio between query rating and its position.

We also define poor query percentage as:

The percentage of all queries with rating less than 3.

Write an SQL query to find each query_name, the quality and poor_query_percentage.

Both quality and poor_query_percentage should be rounded to 2 decimal places.

Return the result table in any order.

The query 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

 

문제 조건 

  • quality : avg(position/rating )
  • poo_query_percentage : avg(ratio<3)
  • 소수점 이하 2자리로 반올림
  • 임의의 순서

 

SELECT query_name
     , ROUND(AVG(rating / position), 2) quality
     , ROUND(AVG(rating < 3) * 100, 2) poor_query_percentage
FROM Queries
GROUP BY query_name

'SQL > leetcode' 카테고리의 다른 글

[578#] Get Highest Answer Rate Question  (0) 2022.07.01
[1241#] Number of Comments per Post  (0) 2022.07.01
[1173#] Immediate Food Delivery I  (0) 2022.07.01
[574] Winning Candidate  (0) 2022.06.29
[570#_MEDIUM] Managers with at Least 5 Direct Reports  (0) 2022.06.29
'SQL/leetcode' 카테고리의 다른 글
  • [578#] Get Highest Answer Rate Question
  • [1241#] Number of Comments per Post
  • [1173#] Immediate Food Delivery I
  • [574] Winning Candidate
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (325)
      • SQL (271)
        • programmers (27)
        • leetcode (198)
        • hacker rank (27)
        • solvesql (1)
        • 개념 (15)
      • etc (28)
        • 1) (13)
        • 2) (14)
      • error (6)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    시계열데이터분석
    solvesql
    해커랭크
    Hard
    패캠챌린지
    패스트캠퍼스후기
    group by
    직장인자기계발
    easy
    LeetCode
    medium
    파이썬을활용한시계열데이터분석
    직장인인강
    패스트캠퍼스
    프로그래머스
    프리미엄
    SQL
    HACKER_RANK
    MySQL
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1211] Queries Quality and Percentage
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.