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 2005 Forums
 Transact-SQL (2005)
 select max filed from table

Author  Topic 

younas02
Starting Member

28 Posts

Posted - 2012-09-10 : 05:24:14
i have a table which include fields, subjectname,title,marks
a subjectname can have more than one row with different marks , i have to select row with maximum marks.
for example
i have 5 rows with record.

subjectname | title| marks

maths | basic| 66
maths | basic| 89
maths | basic | 73
computer | basic | 39
computer | basic | 78


the result i need is
maths | basic| 89
computer|basic |78

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-10 : 05:30:47
[code]
select *
from
(
select *, row_no = row_number() over (partition by subjectname order by marks desc)
from atable
) d
where d.row_no = 1[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-10 : 05:31:03
select
subjectname,
title,
max(marks) as marks
from YourTable
group by subjectname, title


Too old to Rock'n'Roll too young to die.
Go to Top of Page

younas02
Starting Member

28 Posts

Posted - 2012-09-10 : 06:53:20
this is not working for me.
any other solution plz.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-10 : 07:25:11
1. select subjectname, title, marks from YourTable
2. send the output to a printer
3. take a textmarker and mark the lines that you want manually

will this work for you


Too old to Rock'n'Roll too young to die.
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-09-10 : 09:05:58
cant stop laughing, webfred :)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-09-10 : 09:13:53
Using CTE

DECLARE @MarkSheet TABLE (subjectname VARCHAR(50),title VARCHAR(50),marks INT)
INSERT INTO @MarkSheet VALUES
('maths' , 'basic', 66),
('maths' , 'basic', 89),
('maths' , 'basic' , 73),
('computer' , 'basic' , 39),
('computer' , 'basic' , 78)

;WITH CTE
AS (SELECT subjectname,title,marks,
ROW_NUMBER() OVER (PARTITION BY subjectname ORDER BY marks DESC) AS Toper
FROM @MarkSheet)

SELECT subjectname,title,marks FROM CTE WHERE Toper = 1

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page
   

- Advertisement -