如何将MS Access查询转换为PostgreSql查询?

时间:2011-03-16 18:46:07

标签: sql database ms-access postgresql

我在MS Access数据库中有数据已转移到PostgreSQL数据库中。我想复制我在PostgreSQL中在MS Access中进行的查询。在我的SQL视图中,我的表格是:

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 个答案:

答案 0 :(得分: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; 

分享并享受。

答案 1 :(得分: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;