0

我有三个 MS Access 表。它们Income_StatementsBalance_SheetsCash_Flow_Statements。这些表都共享相同的主键。它是一个包含Ticker,[Year]和的三字段主键Period。当然,我想在不复制主键字段的情况下组合所有这三个表。我也有一个需要满足的日期标准。我需要显示的记录等于或早于我从DateTimePicker被调用的日期中选择的日期dtpDateSelection

这是我到目前为止所拥有的:

Dim year As String = dtpDateSelection.Value.Year
Dim quarter As String = ((dtpDateSelection.Value.Month - 1) \ 3) + 1

Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT Ticker, [Year], Period, Income_Statements.Net_Income, Balance_Sheets.Total_Assets, Cash_Flow_Statements.Net_Cash_Flow_Operating FROM Income_Statements, Balance_Sheets, Cash_Flow_Statements WHERE Period < 5 AND Period <= #" & quarter & "# AND [Year] <= #" & year & "#", con)
Dim quarterlyReader As OleDbDataReader = cmd2.ExecuteReader()
Dim Quarterly As New DataTable

Quarterly.Load(quarterlyReader)
DataGridViewScreen.DataSource = Quarterly
quarterlyReader.Close()

我的WHERE陈述有问题。我怎样才能使这项工作?

4

2 回答 2

3

您的 WHERE 子句以两种方式处理 Period 列 - 作为整数和作为日期(这是哈希符号所暗示的),并且不能两者兼而有之。

于 2013-10-04T19:15:12.637 回答
3

如何将表连接在一起并指定所需的字段:

SELECT i.Ticker, i.[Year], i.Period, i.Net_Income, 
   b.Total_Assets, c.Net_Cash_Flow_Operating 
FROM (Income_Statements AS i 
   INNER JOIN Balance_Sheets AS b 
   ON (i.Ticker = b.Ticker) AND (i.[Year] = b.[Year]) AND (i.Period = b.Period)) 
   INNER JOIN Cash_Flow_Statements AS c 
   ON (b.Ticker = c.Ticker) AND (b.[Year] = c.[Year]) AND (b.Period = c.Period)
WHERE i.Period <= #" & quarter & "# 
   AND i.[Year] <= #" & year & "#"
于 2013-10-04T19:21:26.987 回答