Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Rank the records as Quarter

Author  Topic 

MDiwakar
Starting Member

6 Posts

Posted - 2008-01-12 : 06:49:11
I have a tough time solving a problem.
The problem is
I have a table named MARKS which looks like

Student Marks
Student1 40
Student2 46
Student3 55
Student4 58
Student5 60
Student6 78
Student7 80
Student8 89

The result must be like this.

Student8 and Student7 - Quarter 1(Q1)
Student6 and Student5 - Quarter 2(Q2)
Student4 and Student3 - Quarter 3(Q3)
Student2 and Student1 - Quarter 4(Q4)

Can anyone help me with a query?

Thanks in advance.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-12 : 06:52:20
Can you explain how you are supposed to get the given output? What is basis for the Quarters?
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-01-12 : 19:51:22
"Quarter" is not "Quarter of the year"... it's "quatrile" the OP is looking for... Q1 = top 25% of class... Q2 = next 25 % of class... etc...


--Jeff Moden
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-01-12 : 21:32:57
This will do it...

--===== Create and populate a test table to hold test data
-- This is NOT part of the solution
DECLARE @Marks TABLE (Student VARCHAR(10), Marks INT)
INSERT INTO @Marks
(Student, Marks)
SELECT 'Student1',40 UNION ALL
SELECT 'Student2',46 UNION ALL
SELECT 'Student3',55 UNION ALL
SELECT 'Student4',58 UNION ALL
SELECT 'Student5',60 UNION ALL
SELECT 'Student6',78 UNION ALL
SELECT 'Student7',80 UNION ALL
SELECT 'Student8',89

--===== Declare variable to remember how many students
DECLARE @StudentCount DECIMAL(9,2)

--===== Prepare a working table
DECLARE @Quartile TABLE
(
Rank INT IDENTITY(1,1),
Student VARCHAR(10),
Marks INT
)

--===== Populate the working table in the correct order
INSERT INTO @Quartile
(Student, Marks)
SELECT Student, Marks
FROM @Marks
ORDER BY Marks DESC

--===== Remember how many students there are
SELECT @StudentCount = @@ROWCOUNT

--===== Calulate the Quartile for each student
SELECT *,CEILING((Rank/@StudentCount*100)/25) AS Quartile
FROM @Quartile


--Jeff Moden
Go to Top of Page

MDiwakar
Starting Member

6 Posts

Posted - 2008-01-13 : 00:47:56
Great...It's working......
Jeff, thank you for the timely help.
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-01-13 : 00:59:24
And thank you for the feedback...

--Jeff Moden
Go to Top of Page
   

- Advertisement -