为excel vba组合两个sql查询

时间:2014-07-17 17:32:59

标签: sql excel vba excel-vba

我在excel VBA中编写了一个代码,该代码应该从SQL服务器中提取数据。 “tbl.id”在两个表中有关系。在我的第三栏中,我获得了“现金下降”,在第四栏中,我需要从另一个表中获得该“tbl.id”的总标记。我在我的代码中尝试了这个SUM / GROUP,但我运气不好。如果你看看我的excel vba代码,也许你可以理解更多:

    Sub GetingItFromSnd()
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate1 As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
qdate1 = Range("trddate1").Value
qdate2 = Range("trddate2").Value
Sheets("TblData").Range("A2:J20000").ClearContents
Sheets("TblData").Select
Columns("A:J").AutoFilter
Range("A2").Select
Selection.Activate
Set TargetRange = Range("A2")
Set Conn = New ADODB.Connection
 Conn.Open "driver={SQL Server};" & _
"server=XXsql;database=Csn;"
    Set RecSet = New Recordset
RecSet.Open "SELECT   sht_f.tbl_id, sht_f.s_openclose, sht_f.s_cashdrop,  " & _
"sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop, SUM(hist_markers_per_tbl.TotalMarkers) ,  " & _
"replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(sht_f.tbl_id,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''), " & _
"sht_f.pt_name, sht_f.s_cashdrop/2.1, " & _
"(sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop)/2.1 FROM sht_f sht_f, Csn.dbo.hist_markers_per_tbl hist_markers_per_tbl " & _
"WHERE hist_markers_per_tbl.game_date>='" & qdate1 & "' AND hist_markers_per_tbl.game_date<='" & qdate2 & "' sht_f.game_date>='" & qdate1 & "' " & _
"And sht_f.game_date<='" & qdate2 & "' And sht_f.pt_id<>'" & 99 & "'GROUP BY hist_markers_per_tbl.tbl_id ORDER BY sht_f.pt_name", Conn, , , adCmdText
    TargetRange.CopyFromRecordset RecSet
RecSet.Close
Set RecSet = Nothing
Conn.Close
Set Conn = Nothing
LastRow = Sheets("TblData").Range("A" & Sheets("TblData").Rows.Count).End(xlUp).Row
Columns("A:J").AutoFilter
Sheets("WPU").Select
End Sub

1 个答案:

答案 0 :(得分:0)

这很痛苦,但我设法找到了我的问题的答案。因此想与大家分享。实际上,我发现我正在寻找&#34;子功能&#34;在我的查询中:)这是我的代码的最终版本:

    Sub GetingItFromSnd()
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate1 As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
qdate1 = Range("trddate1").Value
qdate2 = Range("trddate2").Value
Sheets("TblData").Range("A2:J20000").ClearContents
Sheets("TblData").Select
Columns("A:J").AutoFilter
Range("A2").Select
Selection.Activate
Set TargetRange = Range("A2")
Set Conn = New ADODB.Connection
Conn.Open "driver={SQL Server};" & _
"server=XXsql;database=Csn;"
Set RecSet = New Recordset
RecSet.Open "SELECT   sht_f.tbl_id, sht_f.s_openclose, sht_f.s_cashdrop,  " & _
"sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop, " & _
"replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(sht_f.tbl_id,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''), " & _
"sht_f.pt_name, sht_f.s_cashdrop/2.1, " & _
"(sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop)/2.1," & _
"SUM(hist_markers_per_tbl.TotalMarkers) " & _
"FROM hist_markers_per_tbl hist_markers_per_tbl" & _
"WHERE sht_f.table_id= hist_markers_per_tbl.tbl_id AND hist_markers_per_tbl.game_date>='" & qdate1 & "' AND hist_markers_per_tbl.game_date<='" & qdate2 & "' GROUP BY hist_markers_per_tbl.tbl_id) " & _
"FROM sht_f sht_f, Csn.dbo.hist_markers_per_tbl hist_markers_per_tbl " & _
"WHERE hist_markers_per_tbl.game_date>='" & qdate1 & "' AND hist_markers_per_tbl.game_date<='" & qdate2 & "' sht_f.game_date>='" & qdate1 & "' " & _
"And sht_f.game_date<='" & qdate2 & "' And sht_f.pt_id<>'" & 99 & "'GROUP BY hist_markers_per_tbl.tbl_id ORDER BY sht_f.pt_name", Conn, , , adCmdText
TargetRange.CopyFromRecordset RecSet
RecSet.Close
Set RecSet = Nothing
Conn.Close
Set Conn = Nothing
LastRow = Sheets("TblData").Range("A" & Sheets("TblData").Rows.Count).End(xlUp).Row
Columns("A:J").AutoFilter
Sheets("WPU").Select
End Sub