0

我在 hsql 中有一个 CREATE VIEW 查询,但是每当我运行它时,它都会抛出这个错误:

表达式不在聚合或 GROUP BY 列中:AGV.ID

我知道如果没有任何聚合表达式(AVG、SUM、MIN、MAX),GROUP BY 将无法工作,但我无法弄清楚如何修复我的查询..因为每条记录都需要按 manifestID 值分组。

基本上,我试图通过组合 3 组选择查询来创建一个视图。

我尝试使用 distinct 但没有运气,因为如果我有多个选定的列,它将不起作用。此查询在 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 和不使用 GROUP BY 的 mysql 中查询结果的快照:

使用 GROUP BY 查询结果

没有 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
4

1 回答 1

2

使用 HSQLDB 2.2.x 或更高版本:

当您使用 GROUP BY 时,所有选定的列都必须在 GROUP BY 列表中,但任何作为聚合的列除外。在您的示例中,GROUP BY 列表应包含 11 列,而不是一列。

您可以使用 DISTINCT,如SELECT DISTINCT COL1, COLB, COLC, ...没有 group by。DISTINCT 是对 SELECT 列表中的所有列进行分组的快捷方式。

一般来说,GROUP BY 意味着查询应该只为 GROUP BY 列表中的每个列值组合返回一行。允许聚合,因为它们将可能多行的值组合成一个值。

现在,如果您将 GROUP BY 列表中的所有列都包括在内,并且结果有多行具有相同的 manifestID 值,这意味着您不能真正单独对 manifestID 进行分组。

更新:您使用 MySQL 的查询结果表明它在这方面不是很严格。manifestId=bhbhbhbh 有二十多行,其他列的值不一样。然而 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 来选择返回哪一行。

于 2012-07-24T11:24:25.193 回答