Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Ranking
--You create a table that has the StudentCode, SubjectCode, and Marks columns to record mid-year marks for students. --The table has marks obtained by 50 students for various subjects. --You need to ensure that the top half of the students arranged by their average marks must be given a rank of 1 --and the remaining students must be given a rank of 2. Which Transact-SQL query should you use? create table student(StudentCode int, SubjectCode int, Marks int) insert into student values (1,1,10),(1,2,20),(1,3,30) ,(2,1,30),(2,2,40),(2,3,50) ,(3,1,40),(3,2,50),(3,3,50) ,(4,1,40),(4,2,50),(4,3,50) go select StudentCode,dense_rank() over(order by avg(marks) desc) as rank from student group by studentcode SELECT StudentCode as Code, DENSE_RANK() OVER (ORDER BY AVG (Marks) DESC) AS Value FROM student GROUP BY StudentCode SELECT StudentCode as Code, NTILE (2) OVER (ORDER BY AVG (Marks) DESC) AS Value FROM student GROUP BY StudentCode -- get subjectcode,studentcode, marks of those who have maximum marks in each subject select StudentCode , SubjectCode , Marks from( select StudentCode , SubjectCode , Marks,rank() over (partition by subjectcode order by marks desc ) as rank from student) tmp where tmp.rank=1
run
|
edit
|
history
|
help
0
kirthi
db3
how-to-assign-a-random-value-in-a-select-statement-in-sqlserver
Sql varchar to date
stackoverflow-54876809-trying-to-get-an-xml-file-into-sql-table
Co-Related sub query
Pro DB's project
Sql Question 1
products table
REGISTRO_ESTUDIANTES