我正在尝试从两个sql表中连接数据。 Table1应该有列FileID,AccName,number,ut _score,uw_score
表2 应该包含 FileID,标签,数据(包含y / N答案)和AdditionalComments。列标签<下方显示的问题列表/ strong>即可。我需要查看最后三个问题(在LABEL列下)并仅在答案为否
时发布结果这两个表都可以使用与每个帐户名相关联的唯一文件ID进行标识。所以我需要加入只有上面提到的列的表。
Option Explicit
Sub GetDataFromADO()
'Declare variables'
Dim objMyConn As ADODB.Connection
Dim objMyCmd As ADODB.Command
Dim objMyRecordset As ADODB.Recordset
Dim iCols As Integer
Dim tbl As ListObject
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source= .;Initial Catalog=.;Trusted_connection=Yes;Integrated Security=SSPI;"
objMyConn.Open
'Dim sSqL As String
'Set objMyCmd.ActiveConnection = objMyConn
'sSqL = "Select FileID, AccName, number,ut _score,uw_score from AUT.dbo.LIST"
'objMyConn.Execute sSqL
'Set and Excecute SQL Command'
Dim u As String
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = "select AccName, Underwriter, Auditor,UT_Score,Underwriter_Score from Actuarial.dbo.Audit_Checklist"
objMyCmd.CommandText = "Select Label, Data, AdditinalComments from Actuarial.dbo.List_data"
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
'Open Recordset'
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open
For iCols = 0 To objMyRecordset.Fields.Count - 1
Worksheets("Data").Cells(1, iCols + 1).Value = objMyRecordset.Fields(iCols).name
Next
'Copy Data to Excel'
Worksheets("Data").Range("A2").CopyFromRecordset objMyRecordset
End Sub
这是sql查询,我需要包含从表2的 Label 列中选择最后6个值的条件。请为我使用的naive where子句道歉。感谢任何帮助!
SELECT Reports.dbo.tbl1.AccName,Reports.dbo.tbl1.Underwriter,
Reports.dbo.tbl1.Auditor, Reports.dbo.tbl1.UT_Score,
Reports.dbo.tbl1.Underwriter_Score, Reports.dbo.tbl2.Label,Reports.dbo.tbl2.Data, Reports.dbo.tbl2.AdditinalComments
FROM Reports.dbo.tbl1
INNER JOIN Reports.dbo.tbl2
ON Reports.dbo.tbl1.FileID=Reports.dbo.tbl2.FileID;
Where Reports.dbo.tbl2.Label='Is it profitable?' OR 'When is effective date expiring ? ' OR 'Did the community accept?';
**我现在得到的输出**