返回列表 上一主題 發帖

[發問] VBA SQL 取得時間間隔 的其他欄位之平均值

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 4# Scott090

參考一下
    http://www.1keydata.com/tw/sql/sqlalias.html
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 7# Scott090
我使用2003版
  1. Option Explicit
  2. Sub ExcelSQL()
  3.     Dim SQL As String, TimeIntervalStr As Double
  4.     Dim j%, r%
  5.     Dim cnn As ADODB.Connection
  6.     Dim rs As ADODB.Recordset
  7.     Dim ws As Worksheet
  8.     Set ws = ThisWorkbook.Sheets(1)
  9.     Set cnn = New ADODB.Connection
  10.     With cnn
  11.             '.Provider = "Microsoft.ACE.OLEDB.12.0"
  12.           '    .ConnectionString = "Extended Properties= Excel 12.0;" _
  13.                 & "Data Source=" & ThisWorkbook.FullName
  14.         '建議不要 Source=" & ThisWorkbook.FullName 在這資料庫活頁簿中執行此巨集,很耗記憶體
  15.         '**********************************************************
  16.         '2003版 引用:microsoft activex data objects 2.x library
  17.         .Provider = "microsoft.jet.oledb.4.0"
  18.         .ConnectionString = "Extended Properties= Excel 8.0;" _
  19.         & "Data Source=D:\VBA SQL.xls"
  20.         '建議不要 Source=" & ThisWorkbook.FullName 在這資料庫活頁簿中執行此巨集,很耗記憶體
  21.         .Open
  22.     End With
  23.     Set rs = New ADODB.Recordset
  24.     SQL = "select * from [ct$] "
  25.     rs.Open SQL, cnn
  26.     With ws
  27.         .[A1].CopyFromRecordset rs
  28.          TimeIntervalStr = .[A1]   '取得第一個時間
  29.         .UsedRange.Clear
  30.     End With
  31.     Do
  32.         Set rs = New ADODB.Recordset
  33.         SQL = "SELECT CTcode, avg(test.Volts) as Volts平均,avg(test.Hz) as Hz平均 from [CT$] " & _
  34.             " as test where 日期時間 >=" & TimeIntervalStr & " AND 日期時間 <" & CDbl(DateAdd("n", 10, TimeIntervalStr)) & "  Group by CTcode "
  35.         rs.Open SQL, cnn, adOpenStatic, adLockReadOnly
  36.         If rs.RecordCount Then   '讀取紀錄
  37.             With ws
  38.                 If .UsedRange.Count = 1 Then
  39.                     .Cells(1) = "日期 時間"
  40.                     For j = 0 To rs.Fields.Count - 1
  41.                         .Cells(1, j + 2) = rs.Fields(j).Name
  42.                     Next
  43.                 End If
  44.                 r = .Cells(.Rows.Count, 2).End(xlUp).Row
  45.                 .Range("B" & r + 1).CopyFromRecordset rs
  46.                 r = .Cells(.Rows.Count, 2).End(xlUp).Row
  47.                 With .Range("A" & Rows.Count).End(xlUp).Offset(1)
  48.                     .Resize(r - .Row + 1) = DateAdd("n", 0, TimeIntervalStr) & vbLf & DateAdd("n", 10, TimeIntervalStr)
  49.                 End With
  50.             End With
  51.         End If
  52.         TimeIntervalStr = CDbl(DateAdd("n", 10, TimeIntervalStr)) '下一個10分鐘
  53.     Loop Until rs.RecordCount = 0  '無紀錄
  54.     rs.Close
  55.     cnn.Close
  56.     Set rs = Nothing
  57.     Set cnn = Nothing
  58. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 9# Scott090
如ThisWorkbook是資料庫,光是載入就很佔記憶體.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 要批評別人時,先想想自己是否完美無缺。
返回列表 上一主題