Lookup table Queries:
+ ------------- + --------- +
| Column the Name | Type |
+ ------------- + ---- + -----
| query_name | VARCHAR |
| the Result | VARCHAR |
| position | int |
| Rating | int |
+ ------------- + --------- +
this table has no primary key, and may have duplicate rows.
This table contains some query information gathered from the database.
"Position" (position) column has a value from 1 to 500.
"Rating" (Rating) column has a value 1-5. Queries score less than 3 are defined as poor quality of inquiries.
The results of the quality of quality is defined as:
The ratio between the average scores for each query result of its location.
The percentage poor_query_percentage inferior query is:
Score of less than 3 percentage results of the total query results.
Write a set of SQL to find the name (query_name) for each query, the quality (quality) and low-quality inquiries percentage (poor_query_percentage).
Quality (quality) and low-quality inquiries percentage (poor_query_percentage) should be rounded to two decimal places.
Results shown in the following format:
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 |
+------------+-------------------+----------+--------+
Result table:
+------------+---------+-----------------------+
| query_name | quality | poor_query_percentage |
+------------+---------+-----------------------+
| Dog | 2.50 | 33.33 |
| Cat | 0.66 | 33.33 |
+------------+---------+-----------------------+
Dog quality query results to ((5/1) + (5/2) + (1/200)) / 3 = 2.50
inferior percentage of query result of the query Dog (1/3) * 100 = 33.33
Cat quality query results to ((2/5) + (3/3) + (4/7)) / 3 = 0.66
inferior percentage of query result of the query Cat (1/3) * 100 = 33.33
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/queries-quality-and-percentage
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Moderation: Write a set of SQL to find the name (query_name) for each query, the quality (quality) and the percentage of low-quality inquiries (poor_query_percentage). Quality (quality) and low-quality inquiries percentage (poor_query_percentage) should be rounded to two decimal places.
This question may seem complex, but not complicated sql.
Thinking:
The ratio between the average scores for each query result of its location.
rating / position dividing by the number of results.
Score of less than 3 percentage results of the total query results.
Find results divided by the total number of less than 3.
First grouped by name. Then seek / position and, within the group of packets is divided by the number of rating.
Number is less than 3 in each request, the number of divided groups.
Problem solving:
SELECT query_name, ROUND(AVG(unavg_quality), 2) AS quality,
ROUND(AVG(poor_query) * 100, 2) AS poor_query_percentage
FROM
(
SELECT query_name, rating, rating/position AS unavg_quality,
IF(rating >= 3, 0, 1) AS poor_query
FROM Queries
) AS temp
GROUP BY query_name;
-- 练习
select query_name, ROUND(AVG(unavg_quality),2) as quality,
ROUND(AVG(poor_query)*100,2) as poor_query_percenyage
from(
query_name, rating, rating/position AS unavg_quality,
IF(rating >= 3, 0, 1) AS poor_query
FROM Queries
) AS temp
group by query_name;
Method Two:
select query_name, round(avg(rating/position),2) quality,
round((sum(if(rating<3,1,0))/count(*))*100,2) poor_query_percentage
from Queries
group by query_name;
Knowledge points: