I have SQL like this:
SELECT
Mid(Note,
InStr(Note, "device.")-
(
InStr(Note, "device.")-
InStr(Note, "pressure and")
)
+13,
(InStr(Note, "device.") - InStr(Note, "pressure and")) - 14
)
AS [Device],
Count([Device]),
Date_Field & " " & Time_Field AS [DateTime],
EnteredBy
FROM MyLog
WHERE Note LIKE "*removed and*"
GROUP BY [Device]
ORDER BY Date_Field DESC
;
I would like to GROUP BY that custom field [Device] and count how many of each device there are. But the code above gives the error “… not include the specified expression … as part of an aggregate function” for the “… AS [Device]” section.
How can I accomplish this?
Right now the data looks like:
Record1 12/05/12 03:02:12 User2
Record1 12/02/12 01:02:12 User1
Record1 12/01/12 02:02:12 User2
Record2 12/06/12 03:02:12 User2
Record2 12/07/12 03:02:12 User3
But I would like it to look like:
Record1 3
Record2 2
This is the old SQL that works (does not aggregate):
SELECT Mid(Note,
InStr(Note, "device.")-
(
InStr(Note, "device.")-
InStr(Note, "pressure and")
)
+13,
(InStr(Note, "device.") - InStr(Note, "pressure and")) - 14
) AS Device, Date_Field & " " & Time_Field AS [DateTime], EnteredBy
FROM MyLog
WHERE Note LIKE "*removed and*"
ORDER BY Date_Field DESC;
You cannot refer to the alias device in a GROUP BY in MS Access, but you can create a subquery and refer to the alias from that.
This runs for me: