Sql 综合及;计数“;选择。。。。“作为[名称]”;领域
我的SQL是这样的:Sql 综合及;计数“;选择。。。。“作为[名称]”;领域,sql,ms-access-2007,Sql,Ms Access 2007,我的SQL是这样的: 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 & " "
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
;
SELECT Count([Device]), Device, [DateTime], EnteredBy
FROM (
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*" ) t
GROUP BY Device, [DateTime], EnteredBy
ORDER BY Date_Field DESC
;
我想按对自定义字段[Device]进行分组,并计算每个设备的数量。但是上面的代码为“…as[Device]”部分给出了错误“…不包括指定的表达式…作为聚合函数的一部分”
我怎样才能做到这一点
目前的数据如下所示:
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
但我希望它看起来像:
Record1 3
Record2 2
这是可用的旧SQL(不聚合):
您不能在MS Access中引用GROUP BY中的别名设备,但可以创建子查询并从中引用别名 这对我来说是:
SELECT EnteredBy, Device, Count(Device) As CountDev FROM (
SELECT
Mid(Note,InStr(Note,"device.")-
(InStr(Note,"device.")-
InStr(Note,"pressure and"))+13,
(InStr(Note,"device.")-InStr(Note,"pressure and"))-14) AS Device,
MyLog.EnteredBy
FROM MyLog
WHERE MyLog.[Note] Like "*removed and*")
GROUP BY EnteredBy, Device
类似于此,基本上将复杂部分嵌套到子查询中
select dev, count(dev) from
(
SELECT
Mid(Note,
InStr(Note, "device.")-
(
InStr(Note, "device.")-
InStr(Note, "pressure and")
)
+13,
(InStr(Note, "device.") - InStr(Note, "pressure and")) - 14
)
AS dev,
Date_Field & " " & Time_Field AS DateTime,
EnteredBy
FROM MyLog
WHERE Note LIKE "*removed and*"
)
GROUP BY dev
ORDER BY DateTime DESC
您收到该错误是因为设备不是列。也许您可以使用子查询,尽管我承认已经有一段时间没有使用Access了 试着这样做:
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
;
SELECT Count([Device]), Device, [DateTime], EnteredBy
FROM (
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*" ) t
GROUP BY Device, [DateTime], EnteredBy
ORDER BY Date_Field DESC
;
祝你好运。Hmm我的查询通常在分组之前就开始了。当我尝试你的时,它说“传递给子字符串函数的长度参数无效”,我现在得到的是
…不包含指定表达式的查询。。。作为聚合函数的一部分。
其中表达式是Mid(注意…)
部分。此外,我认为我需要删除日期/时间显示,因为它们不会被聚合。奇怪的是,编辑后会出现“无效过程调用”。如果我删除了这些日期/时间(我不需要聚合这些日期/时间),我仍然会得到相同的无效长度参数
ODBC调用失败。是的,您的简单版本可以工作。我怀疑它可能在某些元素中加减和溢出。嗯。你需要检查一下(InStr(注,“设备”)-InStr(注,“压力和”))-14是否大于0。你没有像我之前建议的那样列出可能的设备吗?这真的会容易得多。我正在尝试,并将无效的长度参数传递给子字符串函数。子字符串在没有聚合更改的情况下工作。Hmm给我的SELECT语句包括一个保留字或一个拼写错误或丢失的参数名,或者标点不正确
,这非常令人困惑。您的样本数据不包括“压力和”或“设备”或“移除和”。你有真实的样本数据吗?@Remou我不能提供真实的数据,没有。好的,我想我可能有一个有效的版本。见下文。