我在hsql中有一个CREATE VIEW查询,但每当我运行它时,它都会抛出这个错误:
表达式不在聚合或GROUP BY列中:AGV.ID
我知道如果没有任何聚合表达式(AVG,SUM,MIN,MAX),GROUP BY将无法工作,但我无法弄清楚如何修复我的查询。 因为每条记录都需要按manifestID值进行分组。
基本上,我试图通过组合3组选择查询来创建VIEW。
我尝试使用不同但没有运气,因为如果我有多个选定列,它将无效。 此查询在MYSQL中正常工作。
我的查询:
CREATE VIEW local_view_event_manifest(
manifest_id,
eventId,
eventType,
eventDate,
manifestID,
businessStepStr,
manifestVersion,
externalLocation,
remark,
epcCode,
locationCode
)
AS
SELECT
agm.manifest_id AS manifest_id,
agv.id AS eventId,
'AGGREGATION_EVENT' AS eventType,
agv.event_time AS eventDate,
md.manifest_id AS manifestID,
agv.business_step_code AS businessStepStr,
md.manifest_version AS manifestVersion,
md.external_location AS externalLocation,
md.remark AS remark,
epc.code as epcCode,
bloc.location_code as locationCode
FROM
"local".local_MANIFEST_DATA AS md,
"local".local_AGGREGATION_EVENT AS agv,
"local".local_AGGREGATION_EVENT_EPCS AS agv_epc,
"local".local_EPC AS epc,
"local".local_BUSINESS_LOCATION AS bloc,
"local".local_AGGREGATION_EVENT_MANIFEST_DATA AS agm
WHERE
md.id=agm.manifest_id
AND agv.deleted=0
AND md.deleted=0
AND agv.id=agm.aggregation_event_id
AND agv.id=agv_epc.aggregation_event_id
AND agv.business_location_id=bloc.id
AND bloc.id=agv.business_location_id
AND agv_epc.epc_id=epc.id
GROUP BY agm.manifest_id
UNION
SELECT
om.manifest_id AS manifest_id,
ov.id AS eventId,
'OBJECT_EVENT' AS eventType,
ov.event_time AS eventDate,
md.manifest_id AS manifestID,
ov.business_step_code AS businessStepStr,
md.manifest_version AS manifestVersion,
md.external_location AS externalLocation,
md.remark AS remark,
epc.code as epcCode,
bloc.location_code as locationCode
FROM
"local".local_MANIFEST_DATA AS md,
"local".local_OBJECT_EVENT AS ov,
"local".local_OBJECT_EVENT_EPCS AS ov_epc,
"local".local_EPC AS epc,
"local".local_BUSINESS_LOCATION AS bloc,
"local".local_OBJECT_EVENT_MANIFEST_DATA AS om
WHERE
md.id=om.manifest_id
AND ov.deleted=0
AND md.deleted=0
AND ov.id=ov_epc.object_event_id
AND ov.id=om.object_event_id
AND bloc.id=ov.business_location_id
AND ov_epc.epc_id=epc.id
GROUP BY om.manifest_id
UNION
SELECT
trm.manifest_id AS manifest_id,
trv.id AS eventId,
'TRANSACTION_EVENT' AS eventType,
trv.event_time AS eventDate,
md.manifest_id AS manifestID,
trv.business_step_code AS businessStepStr,
md.manifest_version AS manifestVersion,
md.external_location AS externalLocation,
md.remark AS remark,
epc.code as epcCode,
bloc.location_code as locationCode
FROM
"local".local_MANIFEST_DATA AS md,
"local".local_TRANSACTION_EVENT AS trv,
"local".local_TRANSACTION_EVENT_EPCS AS trv_epc,
"local".local_EPC AS epc,
"local".local_BUSINESS_LOCATION AS bloc,
"local".local_TRANSACTION_EVENT_MANIFEST_DATA AS trm
WHERE
md.id=trm.manifest_id
AND trv.deleted=0
AND md.deleted=0
AND trv.id=trv_epc.transaction_event_id
AND trv.id=trm.transaction_event_id
AND bloc.id=trv.business_location_id
AND trv_epc.epc_id=epc.id
GROUP BY trm.manifest_id
下面是使用GROUP BY& amp;的mysql中查询结果的快照。没有GROUP BY:
Ť
... @fredt 感谢您的详细解释..参考您的建议,我已经尝试过了..但不知怎的,我得到了这个错误:
错误:找不到表:语句中的TABL_B [SELECT TABL_B。* FROM(SELECT DISTINCT manifest_id FROM“local”.local_AGGREGATION_EVENT_MANIFEST_DATA)TABL_A]错误代码:-22
以下是我的询问:
SELECT TABL_B.* FROM (SELECT DISTINCT manifest_id FROM "local".local_AGGREGATION_EVENT_MANIFEST_DATA) TABL_A
LATERAL JOIN
( SELECT
agm.manifest_id AS manifest_id,
agv.id AS eventId,
'AGGREGATION_EVENT' AS eventType,
agv.event_time AS eventDate,
md.manifest_id AS manifestID,
agv.business_step_code AS businessStepStr,
md.manifest_version AS manifestVersion,
md.external_location AS externalLocation,
md.remark AS remark,
epc.code as epcCode,
bloc.location_code as locationCode
FROM
"local".local_MANIFEST_DATA AS md,
"local".local_AGGREGATION_EVENT AS agv,
"local".local_AGGREGATION_EVENT_EPCS AS agv_epc,
"local".local_EPC AS epc,
"local".local_BUSINESS_LOCATION AS bloc,
"local".local_AGGREGATION_EVENT_MANIFEST_DATA AS agm
WHERE
md.id=agm.manifest_id
AND agv.deleted=0
AND md.deleted=0
AND agv.id=agm.aggregation_event_id
AND agv.id=agv_epc.aggregation_event_id
AND agv.business_location_id=bloc.id
AND bloc.id=agv.business_location_id
AND agv_epc.epc_id=epc.id AND manifest_id = TABL_A.manifest_id LIMIT 1 ) TABL_B
谢谢@fredt ..我注意到逗号上的内容已经添加到我的查询中了。我也尝试删除JOIN字。但仍然抛出相同的错误..
ERROR: Table not found in statement [SELECT TABL_B.* FROM (SELECT
DISTINCT MANIFEST_ID FROM "local".local_AGGREGATION_EVENT_MANIFEST_DATA)
AS TABL_A, LATERAL] Error Code: -22
答案 0 :(得分:1)
使用HSQLDB 2.2.x或更高版本:
使用GROUP BY时,除了作为聚合的任何列之外,所有选定列都必须位于GROUP BY列表中。在您的示例中,GROUP BY列表应包含11列,而不是一列。
您可以使用DISTINCT,如SELECT DISTINCT COL1, COLB, COLC, ...
中没有分组依据。 DISTINCT是用于对SELECT列表中的所有列进行分组的快捷方式。
通常,GROUP BY表示查询应仅为GROUP BY列表中的每个列值组合返回一行。允许聚合,因为它们将可能多行的值组合成一个值。
现在,如果您在GROUP BY列表中包含所有列,并且结果具有多个具有相同manifestID值的行,则意味着您无法单独对manifestID进行分组。
更新:您使用MySQL的查询结果表明它在这方面不是很严格。 manifestId = bhbhbhbh有超过20行,其他列中的值不相同。然而MySQL随机返回其中一行。这不是GROUP BY如何根据其他数据库(包括HSQLDB)支持的SQL标准工作。请参阅MySQL专家撰写的博客:
http://www.mysqlperformanceblog.com/2006/09/06/wrong-group-by-makes-your-queries-fragile/
如果您想要类似MySQL输出的内容,那么您需要这样的查询:
SELECT TABL_B.* FROM (SELECT DISTINCT manifestID FROM "local".local_AGGREGATION_EVENT_MANIFEST_DATA) TABL_A,
LATERAL
( [YOUR SELECT STATEMENT WITHOUT GROUP BY] AND manifestID = TABL_A.manifestID LIMIT 1 ) TABL_B
首先尝试使用VIEW中的一个选项,然后应用于其余部分。
您可以通过在LIMIT 1之前添加ORDER BY COL_NAME来选择返回哪一行。