Last 7 records

A

AlanW

Could someone please show me how to extract the last 7 records from the code
below

SELECT Table1.A, Table1.B
FROM Table1
ORDER BY Table1.A DESC;

Thanks
 
M

Maurice

Maybe this will do the trick:

SELECT Top 7 Table1.A, Table1.B
FROM Table1
ORDER BY Table1.A DESC;

hth
 
R

RDunlap

Since he wanted the LAST 7 records, shouldn't you change the Order By to
Ascending?
 
R

Rick Brandt

RDunlap said:
Since he wanted the LAST 7 records, shouldn't you change the Order By
to Ascending?

That would totally depend on what value is in the field "A" and how that
relates to "Last" and "First". Generically one would expect to sort in
descending order to get "Last" and ascending order to get "First", but
again, it would depend on the data.
 
K

Klatuu

Also, you may not get exactly 7 records. If there can be duplicate values,
it will return all the records that match the top 7 values. For example

333
252
252
187
155
155
155
155
105
99
99
87
62
62
54
32
32
 
R

Rick Brandt

Klatuu said:
Also, you may not get exactly 7 records. If there can be duplicate
values, it will return all the records that match the top 7 values.
For example

333
252
252
187
155
155
155
155
105
99
99
87
62
62
54
32
32

Not quite right. You get more than 7 records from "Top 7..." only if there
are ties on the 7th value. You don't get all records matching the top 7
unique values as you have listed there. You would get...

333
252
252
187
155
155
155
155
 
K

Klatuu

Rick,
You may be right, but I seem to remember having to resolve this issue once
before and as I recall, I got something more like what I posted. Now I
actually have to go do some work to test it.
 
B

BeWyched

SELECT Table1.A, Table1.B
FROM Table1
GROUP BY Table1.A
ORDER BY Table1.A DESC;

will give the top 7 unique entries.

BW
 
M

Maurice

Hold on, and what if you have 200 records which are all unique where do you
get the 7 in your statement?
 
B

BeWyched

Sorry - forgot a bit!

Should read:

SELECT Top 7 Table1.A, Table1.B
FROM Table1
GROUP BY Table1.A
ORDER BY Table1.A DESC;
 
Top