The PADS [HackerRank]
Problem Link
https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=true
문제 설명
Generate the following two result sets:
Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
Note: There will be at least two entries in the table for each type of occupation.
Input Format
The OCCUPATIONS table is described as follows:
입력 포맷
샘플 입력
샘플 출력
Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.
답안 (MySQL)
SELECT CONCAT(name, '(',SUBSTRING(occupation,1,1),')') FROM OCCUPATIONS ORDER BY name;
SELECT CONCAT('There are a total of ', COUNT(occupation) ,' ',LOWER(occupation),'s.')FROM OCCUPATIONS GROUP BY occupation ORDER BY COUNT(occupation);
해설
SELECT CONCAT(name, '(',SUBSTRING(occupation,1,1),')') FROM OCCUPATIONS ORDER BY name;
SELECT CONCAT('There are a total of ', COUNT(occupation) ,' ',LOWER(occupation),'s.')FROM OCCUPATIONS GROUP BY occupation ORDER BY COUNT(occupation);
CONCAT()
: 둘 이상의 문자열이나 컬럼 값을 하나의 문자열로 합치는 함수. 파라미터 개수는 2개를 넘어도 된다.
SUBSTRING(string_expression, start,length)
: 문자열에 대해 start 지점(인덱스)부터 length만큼 자른다. (start는 양수 또는 음수. 양수인 경우 1부터 시작해서 끝까지, 음수 인경우 뒤에서부터 자름)
LOWER()
: 소문자로 바꾸는 함수.
참고 자료
https://www.w3schools.com/sql/func_mysql_substring.asp
Author And Source
이 문제에 관하여(The PADS [HackerRank]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@dasd412/The-PADS저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)