|
-
Nov 20th, 2018, 05:06 AM
#1
Re: How to design this query for monthly report?
I'm not convinced it makes sense to divvy up Date values into 3 Long values, but whatever floats your boat. Not sure I'd bother with UDTs at all. I'd just keep the Recordset and use it until finished with it.
What about a temporary table?
Input data.txt:
Code:
ID,Pieces,dtDatetime
8,559,9/30/2018 6:50:00 PM
9,581,10/1/2018 6:50:00 AM
10,524,10/1/2018 6:50:00 PM
11,596,10/2/2018 6:50:00 AM
10,395,10/2/2018 6:50:00 PM
67,520,10/30/2018 6:50:00 AM
68,224,10/30/2018 6:50:00 PM
69,529,10/31/2018 6:50:00 AM
70,224,10/31/2018 6:50:00 PM
71,496,11/1/2018 6:50:00 AM
72,395,11/1/2018 6:50:00 PM
Logic:
Code:
Dim Connection As ADODB.Connection
Dim I As Long
On Error Resume Next
Kill "temp-data.txt"
On Error GoTo 0
Set Connection = New ADODB.Connection
With Connection
.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Extended Properties='Text;CSVDelimited=True;Hdr=True';" _
& "Data Source='.'"
.Execute "SELECT " _
& "DateValue([dtDatetime]) AS [Date]," _
& "IIf( TimeValue([dtDatetime]) >= #7:00:00 AM#" _
& " And TimeValue([dtDatetime]) < #7:00:00 PM#," _
& " [Pieces], 0) AS [DayShiftVal]," _
& "IIf( TimeValue([dtDatetime]) >= #7:00:00 PM#" _
& " Or TimeValue([dtDatetime]) < #7:00:00 AM#," _
& " [Pieces], 0) AS [NightShiftVal] " _
& "INTO [temp-data.txt] FROM [data.txt]", _
, _
adCmdText Or adExecuteNoRecords
With New ADODB.Recordset
.CursorLocation = adUseClient
.Open "SELECT " _
& "First([Date]) AS [Date]," _
& "Sum([DayShiftVal]) AS [DayShiftVal]," _
& "Sum([NightShiftVal]) AS [NightShiftVal] " _
& "FROM [temp-data.txt] GROUP BY [Date] ORDER BY [Date]", _
Connection, _
adOpenStatic, _
adLockReadOnly
For I = 0 To .Fields.Count - 1
Debug.Print .Fields(I).Name;
If I < .Fields.Count - 1 Then
Debug.Print ", ";
Else
Debug.Print
End If
Next
Debug.Print .GetString(adClipString, , ", ", vbNewLine)
.Close
End With
.Execute "DROP TABLE [temp-data.txt]", _
, _
adCmdText Or adExecuteNoRecords
.Close
End With
On Error Resume Next
Kill "schema.ini"
On Error GoTo 0
Results:
Code:
Date, DayShiftVal, NightShiftVal
9/30/2018, 559, 0
10/1/2018, 524, 581
10/2/2018, 395, 596
10/30/2018, 224, 520
10/31/2018, 224, 529
11/1/2018, 395, 496
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|