I would like to return the last record in a group and the previous row proceeding the last row.  I was wondering if anyone new how to use ranking or partioning to solve this problem in sql statement.
example 1
member       date        amount
1111            01/14         20.00
1111            02/14         15.00
1111            03/14         50.00
2222            07/13         10.00
2222            01/14         15.00
3333            01/08         20.00
3333            01/09         50.00
3333            01/12         75.00
3333            01/13         80.00

Results
member       date        amount
1111            02/14         15.00
1111            03/14         50.00

2222            07/13         10.00
2222            01/14         15.00

3333            01/12         75.00
3333            01/13         80.00
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Data EngineerCommented:
try this.
``````SELECT t2.member,t2.date,t2.amount
FROM (SELECT t1.*,
ROW_NUMBER() OVER (PARTITION BY t1.member ORDER BY t1.date DESC)
FROM your_table t1) t2
WHERE t2.rn <= 2
``````

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Database AnalystCommented:
You mean something like this?

create table test_table (member int, crtdate Date, amount money)

insert into test_table select 1111, '01/01/14', 20.00
insert into test_table select 1111, '02/01/14', 15.00
insert into test_table select 1111, '03/01/14', 50.00

insert into test_table select 2222, '07/01/13', 10.00
insert into test_table select 2222, '01/01/14', 15.00
insert into test_table select 3333, '01/01/08', 20.00
insert into test_table select 3333, '01/01/09', 50.00
insert into test_table select 3333, '01/01/12', 75.00
insert into test_table select 3333, '01/01/13', 80.00

select * from
(
SELECT t2.rn,t2.member,t2.crtdate,t2.amount
FROM (SELECT t1.member,t1.crtdate,t1.amount,
ROW_NUMBER() OVER (PARTITION BY t1.member ORDER BY t1.crtdate) as rn
FROM test_table t1) t2
) tl
WHERE rn > (select (max(rn) - 2) from
(
SELECT t2.rn,t2.member,t2.crtdate,t2.amount
FROM (SELECT t1.member,t1.crtdate,t1.amount,
ROW_NUMBER() OVER (PARTITION BY t1.member ORDER BY t1.crtdate) as rn
FROM test_table t1) t2
) b
WHERE member = tl.member GROUP BY member)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.