Ofer said:
Copy and paste the SQL I provided you with, change the name of the fields and
the table if needed, it worked for me.
Unless as I said in the second post, if the date field inlude also the time
then you need to use the DateValue
Substituting the names/data (i.e. no time elements) the OP posted,
here's one query:
SELECT DT1.EngineerID, COUNT(*) AS DaysWorked,
SUM(DT1.JobsEachVisitDate) AS Jobs
FROM
(
SELECT W1.EngineerID, W1.VisitDate, COUNT(*) AS JobsEachVisitDate
FROM WorkJobs AS W1
GROUP BY W1.EngineerID, W1.VisitDate
) AS DT1
GROUP BY DT1.EngineerID;
And here's some VBA to recreate the data and demo the SQL code:
Sub workjobs()
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DropMe.mdb"
With .ActiveConnection
' Note: this table is missing a key!
.Execute _
"CREATE TABLE WorkJobs (EngineerID INTEGER" & _
" NOT NULL, VisitDate DATETIME NOT NULL," & _
" CONSTRAINT VisitDate__no_time_part CHECK(" & _
" HOUR(VisitDate) = 0 AND MINUTE(VisitDate)" & _
" = 0 AND SECOND(VisitDate) = 0)); "
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (123, #2006-07-10 00:00:00#);"
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (123, #2006-07-10 00:00:00#);"
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (123, #2006-07-09 00:00:00#);"
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (123, #2006-07-09 00:00:00#);"
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (123, #2006-07-08 00:00:00#);"
.Execute _
"INSERT INTO WorkJobs (EngineerID, VisitDate)" & _
" VALUES (1234, #2006-07-10 00:00:00#);"
Dim rs
Set rs = .Execute( _
"SELECT DT1.EngineerID, COUNT(*) AS DaysWorked," & _
" SUM(DT1.JobsEachVisitDate) AS Jobs FROM" & _
" (SELECT W1.EngineerID, W1.VisitDate, COUNT(*)" & _
" AS JobsEachVisitDate FROM WorkJobs AS W1" & _
" GROUP BY W1.EngineerID, W1.VisitDate) AS" & _
" DT1 GROUP BY DT1.EngineerID;")
Dim sCols As String
Dim f
For Each f In rs.Fields
sCols = sCols & f.Name & vbTab
Next
sCols = Left$(sCols, Len(sCols) - Len(vbTab))
MsgBox sCols & vbCr & rs.GetString(2, , vbTab & vbTab)
rs.Close
End With
Set .ActiveConnection = Nothing
End With
End Sub
Jamie.
--