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

发布于 2024-10-22 01:38:06 字数 1218 浏览 3 评论 0原文

我将 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 中执行此查询时,我收到语法错误。

I have data in an MS Access Database that I have transferred into a PostgreSQL database. I would like to replicate the queries that I have made in MS Access in PostgreSQL. In my SQL view of one my my tables I have:

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;

The trouble is when I try to execute this query in PostgreSQL I receive syntax errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2024-10-29 01:38:06

不确定 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; 

分享并享受。

Not sure about PostgreSQL, but in Oracle you'd change the square braces to double quotes. In addition, it may complain because you're grouping based on columns that aren't in your result set. You may need to change your query to return all the group-by fields as well, like:

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; 

Share and enjoy.

长不大的小祸害 2024-10-29 01:38:06

您的列名称都相同吗?

我面前没有 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;

Are your columns all named the same?

I don't have a postgresql db in front of me, but a generalized sql query would look similar to this. Note that I removed the () around the inner joins and the [] that access uses. In addition, you're grouping by fields that aren't in the SELECT clause of the query - are you sure this is what you're trying to do?

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文