SQL Project
SQL Project
-- Q3) Find the total number of stages crossed at each diffuculty level
-- where for Level2 with players use zm_series devices. Arrange the result
-- in decsreasing order of total number of stages crossed.
select Difficulty as Diff_Level,count(Stages_crossed) as Total_Stages
from level_details2
where Level=2 and Dev_ID like 'zm%'
group by Difficulty
order by Total_Stages desc
-- Q4) Extract P_ID and the total number of unique dates for those players
-- who have played games on multiple days.
select P_ID, count(distinct(Start_datetime)) as Unique_Dates
from level_details2
group by P_ID
HAVing count(distinct(Start_datetime)) > 1
order by Unique_Dates desc
-- Q5) Find P_ID and level wise sum of kill_counts where kill_count
-- is greater than avg kill count for the Medium difficulty.
select P_ID,Level,Sum(Kill_Count) as Total,AVG(Kill_Count) AS Avg_Kills
from level_details2
where Difficulty='Medium'
group by P_ID,Level
having Sum(Kill_Count)>AVG(Kill_Count)
order by Level desc
-- Q6) Find Level and its corresponding Level code wise sum of lives earned
-- excluding level 0. Arrange in asecending order of level.
select l.Level,p.L1_Code,p.L1_Code,SUM(l.Lives_Earned) over (order by l.Level) as
totallives
from player_details p
join level_details2 l
on p.P_ID=l.P_ID
where l.Level<>0
-- Q7) Find Top 3 score based on each dev_id and Rank them in increasing order
-- using Row_Number. Display difficulty as well.
--هنا انا قسمت كل قسم خاص لالي دي ورتبت كل صف جواه علي االسكور
SELECT Score,Dev_ID,ROW_NUMBER() over (partition by Dev_ID order by Score) as rownum
from level_details2
--رتبت كل صف باالسكور بتاعه
SELECT Score,Dev_ID,RANK() over (order by Score) as Rank,
DENSE_RANK() over (order by Score) as rownum
from level_details2
-- Q9) Find Top 5 score based on each difficulty level and Rank them in
-- increasing order using Rank. Display dev_id as well.
select top 5 Dev_ID,Score,Difficulty,
RANK() over (partition by Difficulty order by Score) as Rank,
DENSE_RANK() over (partition by Difficulty order by Score) as DENCERank,
ROW_NUMBER() over (partition by Difficulty order by Score) as rownum
from level_details2
-- Q14) Extract top 3 highest sum of score for each device id and the corresponding
player_id
select top 3 Dev_ID,P_ID,SUM(Score) as score
from level_details2
group by Dev_ID,P_ID
order by score desc
-- Q15) Find players who scored more than 50% of the avg score scored by sum of
-- scores for each player_id
WITH PlayerScores AS (
SELECT P_ID, SUM(Score) AS Total_Score
FROM level_details2
GROUP BY P_ID
),
AvgScores AS (
SELECT AVG(Total_Score) AS Avg_Score
FROM PlayerScores)
SELECT ps.P_ID, ps.Total_Score, a.Avg_Score
FROM PlayerScores ps, AvgScores a
WHERE ps.Total_Score > 0.5 * a.Avg_Score;
-- Q16) Create a stored procedure to find top n headshots_count based on each dev_id and
Rank them in increasing order
-- using Row_Number. Display difficulty as well
RETURN @totalScore;
END;
SELECT dbo.GetTotalScoreForPlayer(632) as Total_Score