SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Retrives the subject wise highest marks list in a table


Retrives the subject wise highest marks list in a table

Author
Message
Radh
Radh
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 109
Hi All,

I have a requirement like, we are having two tables in our database.
Table names: student, marklist

Student table values:

id studname
------------------
1 x
2 y
3 z
4 a
5 b

Marklist table values:

id maths physics English
---------------------------------
1 50 60 70
2 70 60 40
3 50 80 70
4 50 100 70
5 90 60 70

But my requirement is, I need to display the data "subject wise" highest marks for each student.

for example:

id name highestmark
---------------------------------
1 x English


Any boxy help me how to reach this scenario.


Thanks in Advance

Best regards
Radh
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65502 Visits: 17980
This looks a lot like homework. What have you tried so far?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
srikant maurya
srikant maurya
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1503 Visits: 1061
Check Below SQL and let me know if it's work for you


use tempdb;
go
create table stu( id int , name varchar(10))
insert into stu(id,name)
select 1 id, 'x' name union all
select 2, 'y' union all
select 3 , 'z' union all
select 4, 'a' union all
select 5, 'b'

create table mark(id int ,maths int, physics int ,English int)

insert into mark
select 1, 50, 60, 70 union all
select 2 ,70 ,60, 40 union all
select 3, 50, 80, 70 union all
select 4 ,50 ,100, 70 union all
select 5 ,90, 60, 70


go
SELECT id,
subject,
MARK,
Row_number() OVER ( ORDER BY id, MARK DESC ) AS row_id
into #t
FROM
(SELECT id,
Convert(VARCHAR(15), subject) [subject],
MARK
FROM
(SELECT id,
maths,
physics,
english
FROM
MARK) p UNPIVOT (MARK FOR [subject] IN (maths, physics, english) )AS unpvt) t

GO

SELECT t.*
FROM
#t t
INNER JOIN (SELECT id,
Min(row_id) row_id
FROM
#t
GROUP BY id)tt
ON t.row_id = tt.row_id




srikant maurya
srikant maurya
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1503 Visits: 1061
Check Below SQL and let me know if it's work for you


use tempdb;
go
create table stu( id int , name varchar(10))
insert into stu(id,name)
select 1 id, 'x' name union all
select 2, 'y' union all
select 3 , 'z' union all
select 4, 'a' union all
select 5, 'b'

create table mark(id int ,maths int, physics int ,English int)

insert into mark
select 1, 50, 60, 70 union all
select 2 ,70 ,60, 40 union all
select 3, 50, 80, 70 union all
select 4 ,50 ,100, 70 union all
select 5 ,90, 60, 70


go
SELECT id,
subject,
MARK,
Row_number() OVER ( ORDER BY id, MARK DESC ) AS row_id
into #t
FROM
(SELECT id,
Convert(VARCHAR(15), subject) [subject],
MARK
FROM
(SELECT id,
maths,
physics,
english
FROM
MARK) p UNPIVOT (MARK FOR [subject] IN (maths, physics, english) )AS unpvt) t

GO

SELECT t.*
FROM
#t t
INNER JOIN (SELECT id,
Min(row_id) row_id
FROM
#t
GROUP BY id)tt
ON t.row_id = tt.row_id




Radh
Radh
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 109
Hi srikant maurya

Thanks for your valuable suggestion to acheive the scenario.



Thank you very much

Best Regards
Radh
Thomas Abraham
Thomas Abraham
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3891 Visits: 2256
Tell your teacher to stop showing you problems with poorly designed databases.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65502 Visits: 17980
Thomas Abraham (7/20/2011)
Tell your teacher to stop showing you problems with poorly designed databases.


I think that this is major cause of poorly designed tables in the real world. The teachers harp on normalization and then they give garbage like this as an example. Nothing like teaching bad practices as the student is learning. Makes it harder to understand later why that is so bad.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70359 Visits: 9671
Sean Lange (7/20/2011)
Thomas Abraham (7/20/2011)
Tell your teacher to stop showing you problems with poorly designed databases.


I think that this is major cause of poorly designed tables in the real world. The teachers harp on normalization and then they give garbage like this as an example. Nothing like teaching bad practices as the student is learning. Makes it harder to understand later why that is so bad.


I agree, but then again it gives me more jobs to do after they've put the code to work in prod!
chkri.panati
chkri.panati
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 15
SELECT StudentName,Class,MAX(Marks) AS MAX_Marks FROM Student123
GROUP BY StudentName,Class
ORDER BY Class
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)

Group: General Forum Members
Points: 224091 Visits: 42007
chkri.panati (6/21/2013)
SELECT StudentName,Class,MAX(Marks) AS MAX_Marks FROM Student123
GROUP BY StudentName,Class
ORDER BY Class



I'm pretty sure that's not going to work with the original data and table structure given in the original post.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search