我在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
答案 0 :(得分: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 :(得分: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 :(得分:1)
与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 | | |