2012-07-24 78 views
0

我有HSQL一個CREATE VIEW查詢,但每當我運行它,它拋出我這個錯誤:沒有聚集或GROUP BY列HSQL觀點錯誤:表達不聚集或GROUP BY列

表達:AGV.ID

據我所知,GROUP BY不會沒有任何聚合表達式(AVG,SUM,MIN,MAX)工作,但我無法弄清楚如何解決我的查詢.. ,因爲每個記錄需要按照manifestID值進行分組。

基本上,即時通訊嘗試通過結合3組選擇查詢來創建一個視圖。

我試圖使用不同但沒有運氣,因爲它不會工作,如果我有多個選定的列。 此查詢在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的MySQL查詢&結果的快照,同時不會GROUP BY:

query result with GROUP BY

query result without GROUP BY

牛逼


@fredt ... 感謝詳細的解釋..指的是你的建議,我已經嘗試過了..但不知何故,即時得到這個錯誤:

錯誤:沒有找到表:TABL_B在語句[ 。SELECT TABL_B * FROM(SELECT DISTINCT manifest_id FROM 「本地」 .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 

日anks @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

您可以發佈一些帶有預期結果的示例數據嗎? – Madhivanan 2012-07-24 09:32:48

+0

嗨madhivanan ..我已添加查詢結果快照... – 2012-07-24 13:45:34

+0

我已經添加了我的預期結果快照:請參考此鏈接[查詢結果與GROUP BY] – 2012-07-26 07:07:43

回答

1

隨着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查詢結果顯示它在這方面不是很嚴格。有超過20行的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 

在視圖中選擇的一個嘗試第一,然後適用於其餘的。

您可以通過在限制1之前添加ORDER BY COL_NAME來選擇返回哪一行。

+0

作爲我的評論下面..即時通訊表沒有發現錯誤.. :( – 2012-07-25 04:03:44

+0

感謝所有... 和抱歉的回覆.. 我設法解決它.. 你們是對的.. 我從hsql查詢中刪除了GROUP BY子句並將我的選擇查詢修改爲DISTINCT – 2012-08-10 09:13:04

相關問題