如何将 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 中执行此查询时,我收到语法错误。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定 PostgreSQL,但在 Oracle 中,您可以将方括号更改为双引号。此外,它可能会抱怨,因为您根据结果集中不存在的列进行分组。您可能需要更改查询以返回所有分组依据字段,例如:
分享并享受。
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:
Share and enjoy.
您的列名称都相同吗?
我面前没有 postgresql 数据库,但通用的 sql 查询看起来与此类似。请注意,我删除了内部联接周围的 () 和访问使用的 []。此外,您正在按不在查询的 SELECT 子句中的字段进行分组 - 您确定这就是您想要做的吗?
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?