Solved

teradata sql olap functions

Posted on 2014-10-12
2
264 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
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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 39

Expert Comment

by:lcohan
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now