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


Query to find out latest data of each student


Query to find out latest data of each student

Author
Message
diptidjadhav
diptidjadhav
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: 39
I have data as below

Name--Month----Year--------Marks
A--------8--------2012--------80
A--------7--------2012--------88
A--------3--------2011--------70
B--------8--------2012--------80
B--------7--------2012--------88
B--------3--------2011--------70
C--------3--------2012--------80
C--------5--------2012--------88
C--------7--------2012--------70

I want result like this
Name----Month----Year-------Marks
A----------8--------2012--------80
B----------8--------2012--------80
C----------7--------2012--------70


I want latest mark of each student (latest means by considering year,month). Is it possible in single sql query?
Chandra Sekhara Vyas Dhara
Chandra Sekhara Vyas Dhara
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4205 Visits: 1149
Is there any input parameter for Query?



diptidjadhav
diptidjadhav
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: 39
Hi vyas,
The query provided by you gives an error. you can run your query try with below table structure & data.

create table tblStudent
(
Name varchar(100),
Month int,
Year int,
Mark int
)

insert into tblStudent(Name,Month,Year,Mark) values ('A',8,2012,80)
insert into tblStudent(Name,Month,Year,Mark) values ('A',7,2012,88)
insert into tblStudent(Name,Month,Year,Mark) values ('A',3,2011,70)
insert into tblStudent(Name,Month,Year,Mark) values ('B',8,2012,80)
insert into tblStudent(Name,Month,Year,Mark) values ('B',7,2012,88)
insert into tblStudent(Name,Month,Year,Mark) values ('B',3,2011,70)
insert into tblStudent(Name,Month,Year,Mark) values ('C',3,2012,80)
insert into tblStudent(Name,Month,Year,Mark) values ('C',5,2012,88)
insert into tblStudent(Name,Month,Year,Mark) values ('C',7,2012,70)
diptidjadhav
diptidjadhav
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: 39
Please
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19551
;WITH CTE AS (
SELECT
Name,
[Month],
[Year],
Mark,
rn = ROW_NUMBER() OVER(PARTITION BY Name ORDER BY <<Something>>Wink
FROM tblStudent
)
SELECT
Name,
[Month],
[Year],
Mark
FROM CTE
WHERE rn = 1



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jinxseee
Jinxseee
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 159
In future will be careful not to tread on toes!
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19551
Jinxsee (9/27/2012)

select
VALUE, YEAR, MONTH, MARK from
(select VALUE, YEAR, MONTH, MARK, ROW_NUMBER() OVER(PARTITION BY VALUE ORDER by YEAR desc, MONTH desc) as rownumber from TABLE
group by VALUE,YEAR, MONTH, MARK) as a
where a.rownumber = 1




Excellent - but it's customary on ssc to encourage folks to put some effort into their homework. You may be sitting next to diptidjadhav in a couple of years' time, knowing that he used to post his homework on ssc then go to the pub whilst us folks here came up with all his answers!

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jinxseee
Jinxseee
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 159
My bad! Mental note added. ^^.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19551
Jinxsee (9/27/2012)
My bad! Mental note added. ^^.


No worries - now how about fixing your code so that it runs against the OP's sample data? You don't need the GROUP BY.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
diptidjadhav
diptidjadhav
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: 39
Hey thanks Jinxsee and ChrisM@Work. Your solution works perfect. Thanks a lot.
One more question for Jinxsee, Suppose if we are using row_number() for a huge records it will minimize performance?
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