SELECT CASE WHEN a1.Id>1 THEN 'OK' ELSE 'NOT OK' END AS [ID OK/NOT] FROM (SELECT CASE WHEN Id=1 THEN 2 ELSE Id END AS Id FROM tbl_Table WHERE Id>0) a1
First Letter Concatenation:
SELECT CONVERT(nvarchar(10), Id)+' ('+LEFT(Name,1)+')' AS ID_NAME FROM tbl_Table
PIVOT:
SELECT [2016-06-16],[2015-01-01]
FROM (
SELECT
ModuleId,ModuleName,CreateDate
FROM s_Module
) as m
PIVOT
(
MAX(ModuleName)
FOR [CreateDate] IN ([2016-06-16], [2015-01-01])
) AS pvt
Exe 01:
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
Note: Print NULL when there are no more names corresponding to an occupation.
Input Format
The OCCUPATIONS table is described as follows:Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Sample Input
Sample Output
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
Explanation
The first column is an alphabetically ordered list of Doctor names.
The second column is an alphabetically ordered list of Professor names.
The third column is an alphabetically ordered list of Singer names.
The fourth column is an alphabetically ordered list of Actor names.
The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.
The second column is an alphabetically ordered list of Professor names.
The third column is an alphabetically ordered list of Singer names.
The fourth column is an alphabetically ordered list of Actor names.
The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.
SELECT
[Doctor], [Professor], [Singer], [Actor]
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY OCCUPATION ORDER BY NAME) [RowNumber], * FROM OCCUPATIONS
) AS tempTable
PIVOT
(
MAX(NAME) FOR OCCUPATION IN ([Doctor], [Professor], [Singer], [Actor])
) AS pivotTable
Ex-02:
Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
Input Format
The following tables contain contest data:
- Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
- Difficulty: The difficult_level is the level of difficulty of the challenge, and score is the score of the challenge for the difficulty level.
- Challenges: The challenge_id is the id of the challenge, the hacker_id is the id of the hacker who created the challenge, and difficulty_level is the level of difficulty of the challenge.
- Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge that the submission belongs to, and score is the score of the submission.
SOLUTION:
SELECT a.hacker_id, d.name FROM Submissions AS a INNER JOIN Challenges AS b ON b.challenge_id=a.challenge_id INNER JOIN Difficulty AS c ON c.difficulty_level=b.difficulty_level INNER JOIN Hackers AS d ON d.hacker_id=a.hacker_id WHERE a.score=c.score GROUP BY a.hacker_id, d.name HAVING COUNT(d.name) >1 ORDER BY COUNT(d.name) DESC, a.hacker_id ASC;
No comments:
Post a Comment