I have a database that stores customer information. what i need to do is show the customers previous rate. this is easy for customers that have a previous rate (by using row_number and selecting row_number 2)but not all customers have a previous rate so i need to select their current rate.
here is an example of the data:
CUST site serv rnk rate effective date
1 1 1 1 154.7 Jan 01, 2010
1 1 1 2 135.99 Jan 01, 2009
2 1 2 1 45.00 Jan 01, 2010
what i am trying to accomplish is:
CUST site serv rnk rate effective date
1 1 2 2 135.99 Jan 01, 2009
2 1 2 1 45.00 Jan 01, 2010
I have tried using row number to give each customer's rate a ranking:
SELECT CUST,site,serv,rate
row_number() OVER (partition by cust,site,serv order by effd desc) rnk
FROM cm19
i guess what i'm looking for is there a way to put in the where clause case when rnk>1 then rnk=2 else rnk=1?
or is there a better way to get the information that i'm looking for?
thanks in advance!
here is an example of the data:
CUST site serv rnk rate effective date
1 1 1 1 154.7 Jan 01, 2010
1 1 1 2 135.99 Jan 01, 2009
2 1 2 1 45.00 Jan 01, 2010
what i am trying to accomplish is:
CUST site serv rnk rate effective date
1 1 2 2 135.99 Jan 01, 2009
2 1 2 1 45.00 Jan 01, 2010
I have tried using row number to give each customer's rate a ranking:
SELECT CUST,site,serv,rate
row_number() OVER (partition by cust,site,serv order by effd desc) rnk
FROM cm19
i guess what i'm looking for is there a way to put in the where clause case when rnk>1 then rnk=2 else rnk=1?
or is there a better way to get the information that i'm looking for?
thanks in advance!