SQL Puzzle challenge

  • Hi Experts,

    I am taking a puzzle challenge from a popular website. For the below scenarios,as mentioned in the website i am getting the exact output for my queries .But I am getting error "Your code did not pass this test case". I want to where I am getting wrong.
    Puzzle URL
    https://www.hackerrank.com/challenges/the-pads/problem

    My Query:
    select
    case
    when occupation='doctor' then name1
    when occupation='singer' then name1
    when occupation='actor' then name1
    when occupation='professor' then name1
    else null end as professions
    from (
    select occupation,name,concat(name,concat('(',(substring(occupation,1,1)),')')) as name1
    from occupations)
    a
    union
    select
    concat(description,' ', summary,' ', profession)as office from
    (select
    description,occupation,profession,count(profession) as summary
    from (
    select
      occupation,name,
      'There are a total of' as description,
      concat(occupation,'s') as profession
    from occupations
    )a
    group by description,occupation,profession
    )
    b

    Scenario:

    Generate the following two result sets:

    1. 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).
    2. 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:https://s3.amazonaws.com/hr-challenge-images/12889/1443816414-2a465532e7-1.pngOccupation will only contain one of the following values: DoctorProfessorSinger or Actor.

    Sample Input

    An OCCUPATIONS table that contains the following records:

    https://s3.amazonaws.com/hr-challenge-images/12889/1443816608-0b4d01d157-2.png

    Sample Output

    Ashely(P)
    Christeen(P)
    Jane(A)
    Jenny(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.

    Explanation

    The results of the first query are formatted to the problem description's specifications. 
    The results of the second query are ascendingly ordered first by number of names corresponding to each profession(), and then alphabetically by profession , and then alphabetically by profession.

    Regards,
    Saravanan



    Saravanan

  • It's asking you to generate two result sets, and you're only generating one.  In trying to combine the two into one, you've added unnecessary complexity to the query.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Thanks Allen... 
    Can you kindly  tell me how to generate two result sets...
    Regards,
    Saravanan

    Saravanan

  • saravanatn - Saturday, November 4, 2017 11:05 PM

    Thanks Allen... 
    Can you kindly  tell me how to generate two result sets...
    Regards,
    Saravanan

    Use 2 SELECT statements in the same query.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thanks Thorn. If possible can you kindly explain with a example...

    Regards,
    Saravanan

    Saravanan

  • For example:
    [Code]SELECT 1;
    SELECT 2;
    GO[/query]
    That's two statements, run in the same session, with 2 result sets.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thanks Thorn.. It worked....

    Saravanan

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply