2012-02-02 95 views
0

相较于此前的纪录我有这样的PostgreSQL的数据库表:的最新记录PostgreSQL中

Client | Rate | StartDate|EndDate  
A  | 1000 | 2005-1-1 |2005-12-31 
A  | 2000 | 2006-1-1 |2006-12-31 
A  | 3000 | 2007-1-1 |2007-12-31 
B  | 5000 | 2006-1-1 |2006-12-31 
B  | 8000 | 2008-1-1 |2008-12-31 
C  | 2000 | 2006-1-1 |2006-12-31 

我想获得最新的变化,这样的表。怎么样?

Client | Rate | StartDate|EndDate |Pre Rate | Pre StartDate |Pre EndDate  
A  | 3000 | 2007-1-1 |2007-12-31 | 2000 | 2006-1-1  |2006-12-31 
B  | 8000 | 2008-1-1 |2008-12-31 | 5000 | 2006-1-1  |2006-12-31 
C  | 2000 | 2006-1-1 |2006-12-31 

回答

1

我不禁想着有一种更简单的方式来表达这一点。

with current_start_dates as (
    select client, max(startdate) cur_startdate 
    from client_rates 
    group by client 
), 
extended_client_rates as (
    select client, rate, startdate, enddate, 
    lag(rate, 1) over (partition by client order by startdate) prev_rate, 
    lag(startdate,1) over (partition by client order by startdate) prev_startdate, 
    lag(enddate,1) over (partition by client order by startdate) prev_enddate 
    from client_rates 
) 
select cr.* 
from extended_client_rates cr 
inner join current_start_dates csd on csd.client = cr.client 
            and csd.cur_startdate = cr.startdate 
2
SELECT DISTINCT ON (Client) Client, 
          Rate, 
          StartDate, 
          EndDate, 
          LAG(Rate) OVER (PARTITION BY Client 
              ORDER BY StartDate) AS "Pre Rate", 
          LAG(StartDate) OVER (PARTITION BY Client 
               ORDER BY StartDate) AS "Pre StartDate", 
          LAG(EndDate) OVER (PARTITION BY Client 
               ORDER BY StartDate) AS "Pre EndDate" 
FROM ClientRates 
ORDER BY Client, 
     StartDate DESC; 
1

Esentially一样添的回答(+1),与一些抛光和全脚本试图/ cheking

CREATE TEMP TABLE client_rates (client VARCHAR, rate INTEGER, 
    start_date DATE, end_date DATE); 
INSERT INTO client_rates VALUES ('A',1000,'2005-1-1','2005-12-31'); 
INSERT INTO client_rates VALUES ('A',2000,'2006-1-1','2006-12-31'); 
INSERT INTO client_rates VALUES ('A',3000,'2007-1-1','2007-12-31'); 
INSERT INTO client_rates VALUES ('B',5000,'2006-1-1','2006-12-31'); 
INSERT INTO client_rates VALUES ('B',8000,'2008-1-1','2008-12-31'); 
INSERT INTO client_rates VALUES ('C',2000,'2006-1-1','2006-12-31'); 

SELECT DISTINCT ON (client) * FROM 
(
SELECT client, rate, start_date, end_date, 
lag(rate)  OVER w1 AS prev_rate, 
lag(start_date) OVER w1 AS prev_start_date, 
lag(end_date) OVER w1 AS prev_end_date 
FROM client_rates 
WINDOW w1 AS (PARTITION BY client ORDER BY start_date) 
ORDER BY client,start_date desc 
) AS foo; 

client | rate | start_date | end_date | prev_rate | prev_start_date | prev_end_date 
--------+------+------------+------------+-----------+-----------------+--------------- 
A  | 3000 | 2007-01-01 | 2007-12-31 |  2000 | 2006-01-01  | 2006-12-31 
B  | 8000 | 2008-01-01 | 2008-12-31 |  5000 | 2006-01-01  | 2006-12-31 
C  | 2000 | 2006-01-01 | 2006-12-31 |   |     |