0

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.

1
  • looks like the square brackets... did the table names transfer cleanly? Commented Mar 16, 2011 at 18:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.