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.
Input Format
The OCCUPATIONS table is described as follows:
Column | Type |
---|---|
Name | String |
Occupation | String |
one of the following values: Doctor, Professor, Singer or Actor.
Sample Input
An OCCUPATIONS table that contains the following records:
Sample Output
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.
SQL에서의 CONCAT사용법: 이번문제의 경우 전혀 어려운 문제가 아니다. 오히려 훨씬 난이도가 낮은 문제였다. 하지만 CONCAT의 활용을 몰라서 30분정도의 시간이 걸리게 되었고, CONCAT을 사용하게되면 쉽게 풀리게 된다.
SQL에서의 CONCAT은 함수안에 여러개의 문자열 혹은 식을 쉼표단위로 넣어주면된다.
SELECT CONCAT(name,'(',left(Occupation,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), occupation
너무 쉬운문제라서 따로 풀이는 남기지 않겠다. CONCAT만 활용하면 금방 풀 수 있다.