Solved

teradata sql olap functions

Posted on 2014-10-12
2
319 Views
Last Modified: 2014-10-20
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
0
Comment
Question by:centralmike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 40376113
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

Open in new window

0
 
LVL 40

Expert Comment

by:lcohan
ID: 40380133
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)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question