比较postgresql中的最新记录和以前的记录

比较postgresql中的最新记录和以前的记录,postgresql,Postgresql,我在PostgreSQL DB中有这样一个表: 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 |200

我在PostgreSQL DB中有这样一个表:

 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 

我忍不住想,有一种更简单的方式来表达这一点

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

我忍不住想,有一种更简单的方式来表达这一点

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

本质上与Tim的答案(+1)相同,有一些修饰和完整的脚本供尝试/检查

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 |           |                 |

本质上与Tim的答案(+1)相同,有一些修饰和完整的脚本供尝试/检查

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 |           |                 |