Hi
I have a SQL Server 2005 database with a list of results for a variety of people over time. i would like to be able to look at any row and from that row know the last 3 results for that person
the columns are like this
date ----- person ----- result
i would like to be able to add in columns which would show
----- last result ----- last but one result ----- last but two result
i could then look at averages etc so my table (or new table) would be
date ----- person ----- result ----- last result ----- last but one result ----- last but two result
Can anyone help with a way to do this? it would need to find the previous record of the person and get their result and then go to the record of that person before that and get the result etc etc.
thanks in advance and merry christmas.
8 4710
you can get idea from this thread.. -
select top 3 * from
-
(select * from tablename
-
where person = @personid
-
order by date desc) as RS
-
now, your new resultset RS will have last 3 results..
you can join it with your original table.
thanks for the reply but i was not clear enough in my explanation. the person column of the table has thousands of rows with hundreds of different people listsed. I need to find the last results for each and every row in the table. Obviously this won't be possible for some of the early rows in the table as there will not be previous results.
i hope that explanation makes my case clearer.
While there's no reply yet, try this one for now...I'm sure there's a better way of doing it, but I got called at work :) -
-
-
-
with lastrecord (personid, lastdate)
-
as
-
(
-
select personid, max(date) as lastdate
-
from transactiontable group by personid
-
),
-
last2record (personid, last2date)
-
as
-
(
-
select top 1 t.personid, max(t.date) as last2date
-
from transactiontable t
-
inner join lastrecord l on t.personid = l.personid and t.date < l.lastdate
-
group by personid where date
-
),
-
last3record (personid, last3date)
-
as
-
(
-
select t.personid, max(t.date) last3date
-
from transactiontable t
-
inner join last2record l on t.personid = l.personid and t.date < l.last2date
-
group by personid where date
-
)
-
select
-
p.personid, l1.lastdate, l2.last2date, l3.last3date
-
from personstable p
-
left join lastrecord l1 on p.personid = l1.personid
-
left join last2record l2 on p.personid = l2.personid
-
left join last3record l3 on p.personid = l3.personid
-
-
Happy Coding!!!
~CK
Hi ssnaik84,
You cannot use Order by clause inside a subquery otherwise you will encounter the error
Msg 1033, Level 15, State 1, Line 16
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Well nbiswas.. then use TOP... :)
isn't that simple..?? - select top 3 * from
-
(select top (select COUNT(*) from tablename) *
-
from tablename
-
where person = @personid
-
order by date desc) as RS
Yes u r right. Now it looks fine.
only just got back to this what with christmas and all that.
thanks for all the replies. ck9663, I can't seem to get what you have done to work. i get an error saying incorrect syntax near the keyword where on line 13 which is
group by personid where date
any ideas?
also is it right that in just one of the lastrecord sub queries you use select top 1? apologies if i am way off the mark here and thanks for what you have done so far.
Sorry about that.
Try this: -
-
with lastrecord (personid, lastdate)
-
as
-
(
-
select personid, max(date) as lastdate
-
from transactiontable group by personid
-
),
-
last2record (personid, last2date)
-
as
-
(
-
select top 1 t.personid, max(t.date) as last2date
-
from transactiontable t
-
inner join lastrecord l on t.personid = l.personid and t.date < l.lastdate
-
group by personid
-
),
-
last3record (personid, last3date)
-
as
-
(
-
select top 1 t.personid, max(t.date) last3date
-
from transactiontable t
-
inner join last2record l on t.personid = l.personid and t.date < l.last2date
-
group by personid
-
)
-
select
-
p.personid, l1.lastdate, l2.last2date, l3.last3date
-
from personstable p
-
left join lastrecord l1 on p.personid = l1.personid
-
left join last2record l2 on p.personid = l2.personid
-
left join last3record l3 on p.personid = l3.personid
-
-
Happy Coding !!!
The TOP 1 is to only get he first record...
--- CK
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dan Matthews |
last post by:
Hey everybody,
I have an 2000 Access database that stores job listings from potential
employers for a school. I would like to be able to display the date
on a webpage the last time the database...
|
by: Mark |
last post by:
good spam subject ;).
anyway, i'm alittle stumped. i'm in need of putting together a query
that gets the next highest salary ( select max ( sal ) - 1?, from an
emp_sal type table. another...
|
by: Astra |
last post by:
Hi All
I know an SQL Server timestamp seems to be as useful as rocking horse for
show jumping, but I'm hoping you know a 'fudge' to get me round a problem or
at least confirm that it isn't...
|
by: John A Fotheringham |
last post by:
I have a table that tracks GPS records broadly speaking as follows
ts DATETIME
username VARCHAR(16)
...
GPS data
...
I want to select the most recent GPS data for each distinct user.
That...
|
by: pg |
last post by:
Is there any simple way to query the most recent time of "changes" made to a
table?
I'm accessing my database with ODBC to a remote site thru internet. I want
to eliminate some DUPLICATE long...
|
by: etamp |
last post by:
Etamp.net, the fast rss-reader for RSS content including weblogs and news,
the fast search engine for RSS content including weblogs and news,
No software download, All OS' & Browsers, IE, Netscape,...
|
by: kiqyou_vf |
last post by:
I'm trying to pull data from 2 different tables and do a loop to
retrieve more than one row. I'm having problems with aligning the
information. Can someone lead me in the right direction? I've done...
|
by: kiqyou_vf |
last post by:
Sorry, Google wouldn't let me post a reply. Here is the convo thus far:
Jerry Stuckle wrote:
> kiqyou_vf wrote:
>> I'm trying to pull data from 2 different tables and do a loop to
>> retrieve...
|
by: eloi-ribeiro.blogspot.com |
last post by:
Hi,
I'm having a problem with a query. I'm running Access 2000 and a table
with 35000 rows.
The table contains several visits to the spots with a CODE (spot code)
and DATE (visit date)...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |