加入2个表 - 填写缺少的日期

时间:2016-04-29 15:08:47

标签: sql sql-server-2008 left-join inner-join

我无法加入2个表并精确过滤数据。如果需要,我可以稍微修改表。我正在运行Microsoft SQL Server 2008。

Table A
System_Code | Name         | Client ID| Fund   | Purchase_Date  | Shares
1           | Lily         | 123456   | 001    | 02/21/2016     | 100
1           | Lily         | 123456   | 001    | 02/26/2016     | 200
2           | Lily         | 123456   | 002    | 02/24/2016     | 250

等等

每个基金都有自己的System_Code

Table B
System_Code |  Date           | Price
1           |  02/21/2016     | 10
1           |  02/22/2016     | 10 
1           |  02/23/2016     | 9
1           |  02/24/2016     | 10
1           |  02/25/2016     | 11
1           |  02/26/2016     | 10.5

等等

我的目标是了解客户在特定日期持有的份额和每股价格。

如果我只是加入2个表ON(tableA.System_Code = tableB.System_Code),我明白了:

System_Code| Name | Client ID| Fund |Purchase_Date|Shares| Date     | Price 
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/21/2016| 10
1          | Lily | 123456   | 001  |02/26/2016   |200   |02/26/2016| 10.5

如果我将两张桌子连在一起WHERE tableB.Date> = tableA.Purchase

System_Code| Name | Client ID| Fund |Purchase_Date|Shares| Date     | Price 
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/21/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/22/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/23/2016| 9
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/24/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/25/2016| 11
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/26/2016| 10.5
1          | Lily | 123456   | 001  |02/26/2016   |200   |02/26/2016| 10.5

正如你所看到的,除了最后两行外,它几乎是完美的

1          | Lily | 123456   | 001  |02/21/2016   |100   |02/26/2016| 10.5
1          | Lily | 123456   | 001  |02/26/2016   |200   |02/26/2016| 10.5

由于在2016年2月26日进行了购买,因此购买日期为2016年2月21日的第一行应该会下降。期望的结果是:

System_Code| Name | Client ID| Fund |Purchase_Date|Shares| Date     | Price 
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/21/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/22/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/23/2016| 9
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/24/2016| 10
1          | Lily | 123456   | 001  |02/21/2016   |100   |02/25/2016| 11
1          | Lily | 123456   | 001  |02/26/2016   |200   |02/26/2016| 10.5

希望我清楚地传达我的观点。 TIA!

2 个答案:

答案 0 :(得分:0)

您的最终查询应该是这样的:

select *
 from A
  left join B
  on (A.System_Code = B.System_Code)
 where B.Date >= A.Purchase

答案 1 :(得分:0)

试试这个:

SELECT a.*
    ,b.*
FROM dbo.Table_A a
JOIN table_b b ON b.System_Code = a.System_Code
WHERE b.DATE >= a.Purchase_Date

EXCEPT

SELECT a.System_Code
    ,a.NAME
    ,a.Client_ID
    ,a.Fund
    ,a.Purchase_Date
    ,a.Shares
    ,b.System_Code
    ,a2.Purchase_Date AS DATE
    ,b.Price
FROM dbo.Table_A a
JOIN TABLE_a a2 ON a.System_Code = a2.System_Code
    AND a.Purchase_Date < a2.Purchase_Date
JOIN table_b b ON b.System_Code = a.System_Code