PostgreSQL在最后一次加入

时间:2014-09-01 16:23:07

标签: postgresql join

我有两张表:(1)stock_purchase表包含买卖股票的历史; (2)股息表包含在较长时间内为许多股票宣布的股息历史。

我的目标:结合stock_purchase和dividend表,如果股息在股息日期持有,则添加股息。

示例表格: (1)stock_purchase表:两个在不同日期买入和卖出不同股票的账户。

account_name purchase_date  ticker_symbol   purchase_amount   cum_shares_held
john          02/27/2013        GOOG              500             50 
john          04/15/2013        GOOG             -250             40            
john          08/08/2013        GOOG             1000            150
john          03/01/2013        FB                750             20
john          04/15/2013        FB               -500              5
john          09/15/2013        FB               1500             50
bill          02/21/2013        GOOG              500             55 
bill          04/12/2013        GOOG             -250             43            
bill          08/29/2013        GOOG             1000            161
bill          03/17/2013        FB                750             19
bill          04/12/2013        FB               -500              7
bill          09/27/2013        FB               1500             57

现在我们为GOOG和FB提供以下股息历史表。股息每季度宣布一次。

ticker_symbol dividend_date     dividend_amount_per_share
goog          12/31/2012        .04 
goog          03/31/2013        .04 
goog          06/30/2013        .03
goog          09/30/2013        .09
goog          12/31/2013        .05
goog          03/31/2014        .04 
goog          06/30/2014        .03
fb            12/31/2012        .44 
fb            03/31/2013        .19 
fb            06/30/2013        .23
fb            09/30/2013        .25
fb            12/31/2013        .39
fb            03/30/2014        .99
fb            06/30/2014        .79

我尝试了几种左/外连接组合以及插入但我的结果不是我想要的。

至少,如果我能在股票表格中获得额外的股息记录,这将是一个良好的开端。 接下来最好的是:只有那些在我的第一个/最后一个购买日期内的股息记录。

任何想法或想法?感谢您的任何时间和精力。

1 个答案:

答案 0 :(得分:2)

SQL Fiddle

select distinct on (account_name, sp.ticker_symbol, dividend_date)
    account_name as account,
    sp.ticker_symbol as ticker,
    cum_shares_held,
    dividend_date,
    dividend_amount_per_share as dividend_per_share,
    dividend_amount_per_share * cum_shares_held as dividend_total
from
    stock_purchase sp
    inner join
    dividend_history dh on
        lower(sp.ticker_symbol) = dh.ticker_symbol
        and
        sp.purchase_date <= dh.dividend_date
order by account_name, sp.ticker_symbol, dividend_date, purchase_date desc
;
 account | ticker | cum_shares_held | dividend_date | dividend_per_share | dividend_total 
---------+--------+-----------------+---------------+--------------------+----------------
 bill    | FB     |              19 | 2013-03-31    |               0.19 |           3.61
 bill    | FB     |               7 | 2013-06-30    |               0.23 |           1.61
 bill    | FB     |              57 | 2013-09-30    |               0.25 |          14.25
 bill    | FB     |              57 | 2013-12-31    |               0.39 |          22.23
 bill    | FB     |              57 | 2014-03-30    |               0.99 |          56.43
 bill    | FB     |              57 | 2014-06-30    |               0.79 |          45.03
 bill    | GOOG   |              55 | 2013-03-31    |               0.04 |           2.20
 bill    | GOOG   |              43 | 2013-06-30    |               0.03 |           1.29
 bill    | GOOG   |             161 | 2013-09-30    |               0.09 |          14.49
 bill    | GOOG   |             161 | 2013-12-31    |               0.05 |           8.05
 bill    | GOOG   |             161 | 2014-03-31    |               0.04 |           6.44
 bill    | GOOG   |             161 | 2014-06-30    |               0.03 |           4.83
 john    | FB     |              20 | 2013-03-31    |               0.19 |           3.80
 john    | FB     |               5 | 2013-06-30    |               0.23 |           1.15
 john    | FB     |              50 | 2013-09-30    |               0.25 |          12.50
 john    | FB     |              50 | 2013-12-31    |               0.39 |          19.50
 john    | FB     |              50 | 2014-03-30    |               0.99 |          49.50
 john    | FB     |              50 | 2014-06-30    |               0.79 |          39.50
 john    | GOOG   |              50 | 2013-03-31    |               0.04 |           2.00
 john    | GOOG   |              40 | 2013-06-30    |               0.03 |           1.20
 john    | GOOG   |             150 | 2013-09-30    |               0.09 |          13.50
 john    | GOOG   |             150 | 2013-12-31    |               0.05 |           7.50
 john    | GOOG   |             150 | 2014-03-31    |               0.04 |           6.00
 john    | GOOG   |             150 | 2014-06-30    |               0.03 |           4.50