Solved

teradata sql olap functions

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword 2 45
How to join on ID, with prefix? 15 58
SQL NULL vs Blank 26 36
SQL Server 2012 r2 - Sum totals 2 25
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

770 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