title: POSTGRES Group by / Inner Join
body: <p>I am having problems writing a query to summarize the number trips from a OD matrix table.  I am still new to using databases other than MS Access, so please forgive my inexperience. SMGZ is the table with the number of zones and matrix_od is the matrix table with all the OD pairs(4109*4109) from zone to zone with the total number of trip types. I am not sure this is the best way to write this query.  SO what i need to do is summarize 4 types of trips(LHDT,MHDT,HHDT,HDT_Tota) by the OD. Help is much appreciated.</p>
  
  <pre><code>SELECT 
     smgz.smg_zone AS "O_ID",
     smgz.x AS "O_X",
     smgz.y AS "O_Y",
     smgz1.smg_zone AS "D_ID",
     smgz1.x AS "D_X",
     smgz1.y AS "D_Y",
     SUM(matrix_od."LHDT") AS "LHDT_Tot", 
     SUM(matrix_od."MHDT") AS "MHDT_Tot", 
     SUM(matrix_od."HHDT") AS "HHDT_Tot", 
     SUM(matrix_od."TOT_HDT") AS "HDT_Tot"
  FROM 
     public.smgz, 
     public.matrix_od, 
     public.smgz smgz1
  GROUP BY
    "O_ID","O_X","O_Y","D_ID","D_X","D_Y"
  INNER JOIN 
    smgz on matrix_od.O_ID = smgz.ID
  INNER JOIN 
    smgz1 on matrix_od.D_ID = smgz1.ID;
  
  
   ERROR:  syntax error at or near "INNER"
   LINE 18: INNER JOIN 
            ^
  
  ********** Error **********
  
   ERROR: syntax error at or near "INNER"
   SQL state: 42601
   Character: 414
  </code></pre>