2011-03-16 98 views
0

我有一個MS Access數據庫中的數據,我已經轉移到PostgreSQL數據庫。我想複製我在PostgreSQL中使用MS Access進行的查詢。在我的一個我的我的表的SQL視圖中,我有:如何將MS Access查詢轉換爲PostgreSql查詢?

SELECT GENERAL_CAUSE_NFD.[Cause Class], DFS_FIRE_ARCHIVE.FIRE_YEAR AS [Year], OBJECTIVES_NFD.[Response Category], Count(DFS_FIRE_ARCHIVE.REGION) AS Total 
FROM (((DFS_FIRE_ARCHIVE INNER JOIN GENERAL_CAUSE_ORDER ON DFS_FIRE_ARCHIVE.GENERAL_CAUSE = GENERAL_CAUSE_ORDER.GENERAL_CAUSE) INNER JOIN OBJECTIVE_ORDER ON DFS_FIRE_ARCHIVE.OBJECTIVE = OBJECTIVE_ORDER.OBJECTIVE) INNER JOIN OBJECTIVES_NFD ON OBJECTIVE_ORDER.OBJECTIVE = OBJECTIVES_NFD.OBJECTIVE) INNER JOIN GENERAL_CAUSE_NFD ON GENERAL_CAUSE_ORDER.GENERAL_CAUSE = GENERAL_CAUSE_NFD.GENERAL_CAUSE 
GROUP BY GENERAL_CAUSE_NFD.[Cause Class], DFS_FIRE_ARCHIVE.FIRE_YEAR, OBJECTIVES_NFD.[Response Category], DFS_FIRE_ARCHIVE.GENERAL_CAUSE, DFS_FIRE_ARCHIVE.OBJECTIVE, GENERAL_CAUSE_ORDER.ORDER, OBJECTIVE_ORDER.ORDER, DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE, DFS_FIRE_ARCHIVE.FIRE_TYPE 
HAVING (((DFS_FIRE_ARCHIVE.FIRE_YEAR)=2009) AND ((DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE)="INT") AND ((DFS_FIRE_ARCHIVE.FIRE_TYPE)="IFR")) 
ORDER BY GENERAL_CAUSE_ORDER.ORDER, OBJECTIVE_ORDER.ORDER, DFS_FIRE_ARCHIVE.OBJECTIVE; 

問題是當我嘗試在PostgreSQL中執行此查詢時收到語法錯誤。

+2

錯誤說的是什麼? – Nanne 2011-03-16 18:48:10

+0

看起來像方括號......表名是否乾淨地傳輸? – Randy 2011-03-16 18:56:21

回答

0

您的專欄全部命名相同嗎?

我沒有在我面前的postgresql數據庫,但一般化的SQL查詢看起來類似於此。請注意,我刪除了內部連接的()和訪問使用的[]。另外,你按照不在查詢的SELECT子句中的字段進行分組 - 你確定這是你想要做的嗎?

SELECT 
GENERAL_CAUSE_NFD."Cause Class", 
DFS_FIRE_ARCHIVE.FIRE_YEAR AS Year, 
OBJECTIVES_NFD."Response Category", 
Count(DFS_FIRE_ARCHIVE.REGION) AS Total 

FROM 
DFS_FIRE_ARCHIVE INNER JOIN GENERAL_CAUSE_ORDER 
    ON DFS_FIRE_ARCHIVE.GENERAL_CAUSE = GENERAL_CAUSE_ORDER.GENERAL_CAUSE 
INNER JOIN OBJECTIVE_ORDER 
    ON DFS_FIRE_ARCHIVE.OBJECTIVE = OBJECTIVE_ORDER.OBJECTIVE 
INNER JOIN OBJECTIVES_NFD 
    ON OBJECTIVE_ORDER.OBJECTIVE = OBJECTIVES_NFD.OBJECTIVE 
INNER JOIN GENERAL_CAUSE_NFD 
    ON GENERAL_CAUSE_ORDER.GENERAL_CAUSE = GENERAL_CAUSE_NFD.GENERAL_CAUSE 

GROUP BY GENERAL_CAUSE_NFD."Cause Class", 
DFS_FIRE_ARCHIVE.FIRE_YEAR, 
OBJECTIVES_NFD."Response Category", 
DFS_FIRE_ARCHIVE.GENERAL_CAUSE, 
DFS_FIRE_ARCHIVE.OBJECTIVE, 
GENERAL_CAUSE_ORDER.ORDER, 
OBJECTIVE_ORDER.ORDER, 
DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE, 
DFS_FIRE_ARCHIVE.FIRE_TYPE 

HAVING 
    DFS_FIRE_ARCHIVE.FIRE_YEAR=2009 
AND DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE='INT' 
AND DFS_FIRE_ARCHIVE.FIRE_TYPE='IFR' 

ORDER BY GENERAL_CAUSE_ORDER."ORDER", 
     OBJECTIVE_ORDER."ORDER", 
     DFS_FIRE_ARCHIVE.OBJECTIVE; 
1

不確定PostgreSQL,但在Oracle中,您會將方括號改爲雙引號。另外,它可能會因爲您基於不在結果集中的列進行分組而抱怨。您可能需要更改查詢以返回所有分組字段,例如:

SELECT GENERAL_CAUSE_NFD."Cause Class", 
     DFS_FIRE_ARCHIVE.FIRE_YEAR AS "Year", 
     OBJECTIVES_NFD."Response Category", 
     DFS_FIRE_ARCHIVE.GENERAL_CAUSE, 
     DFS_FIRE_ARCHIVE.OBJECTIVE, 
     GENERAL_CAUSE_ORDER.ORDER, 
     OBJECTIVE_ORDER.ORDER, 
     DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE, 
     DFS_FIRE_ARCHIVE.FIRE_TYPE, 
     Count(DFS_FIRE_ARCHIVE.REGION) AS "Total" 
    FROM (((DFS_FIRE_ARCHIVE 
      INNER JOIN GENERAL_CAUSE_ORDER 
      ON DFS_FIRE_ARCHIVE.GENERAL_CAUSE = GENERAL_CAUSE_ORDER.GENERAL_CAUSE) 
     INNER JOIN OBJECTIVE_ORDER 
      ON DFS_FIRE_ARCHIVE.OBJECTIVE = OBJECTIVE_ORDER.OBJECTIVE) 
     INNER JOIN OBJECTIVES_NFD 
      ON OBJECTIVE_ORDER.OBJECTIVE = OBJECTIVES_NFD.OBJECTIVE) 
     INNER JOIN GENERAL_CAUSE_NFD 
      ON GENERAL_CAUSE_ORDER.GENERAL_CAUSE = GENERAL_CAUSE_NFD.GENERAL_CAUSE 
    GROUP BY GENERAL_CAUSE_NFD."Cause Class", 
      DFS_FIRE_ARCHIVE.FIRE_YEAR, 
      OBJECTIVES_NFD."Response Category", 
      DFS_FIRE_ARCHIVE.GENERAL_CAUSE, 
      DFS_FIRE_ARCHIVE.OBJECTIVE, 
      GENERAL_CAUSE_ORDER.ORDER, 
      OBJECTIVE_ORDER.ORDER, 
      DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE, 
      DFS_FIRE_ARCHIVE.FIRE_TYPE 
    HAVING (((DFS_FIRE_ARCHIVE.FIRE_YEAR)=2009) AND 
      ((DFS_FIRE_ARCHIVE.FIRE_MGT_ZONE)="INT") AND 
      ((DFS_FIRE_ARCHIVE.FIRE_TYPE)="IFR")) 
    ORDER BY GENERAL_CAUSE_ORDER.ORDER, 
      OBJECTIVE_ORDER.ORDER, 
      DFS_FIRE_ARCHIVE.OBJECTIVE; 

分享和享受。

+0

謝謝,它現在正在工作。 – Awet 2011-03-16 20:25:49