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 Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 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
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25863 Visits: 17509
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1139 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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1139 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 Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 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
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2703 Visits: 2255
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
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25863 Visits: 17509
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
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28733 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
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 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 (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84869 Visits: 41069
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