Solved

teradata sql olap functions

Posted on 2014-10-12
2
292 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

685 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