3

I have a PostgreSQL 9.2.2 server running for a GWT project.

One query is giving trouble, as it works fine, when I try it in the sql tab in phpPgAdmin:

 (select distinct max(enddate) as max,meterid 
  from usermeasurements 
  where locationid=127025 
    AND usage>-1 
  GROUP BY meterid ) as t1 
  left outer join usermeasurements as t2 
            ON (t1.meterid=t2.meterid 
            AND t1.max=t2.enddate)

But when I try using the query in my gwt project, I get the following error:

 [btpool0-5] ERROR com.example.DataGetter  - unable to get usermeasurement: org.postgresql.util.PSQLException: ERROR: syntax error at or near "as" Position: 112

It's the first time I have experienced a difference between what works in phpPgAdmin and an app.

As can be seen, I use BoneCP to handle my connection pool, but that shouldn't have any effect as far a I know.

5
  • Hint for debugging: turn on full sql logging, so you can see the real sql query stackoverflow.com/questions/722221/… Commented Jan 11, 2013 at 15:28
  • Can you give the full query? Commented Jan 11, 2013 at 15:50
  • @MortenSkov It cant be the full query. It is only the FROM part of the bigger query. Commented Jan 11, 2013 at 16:35
  • @leonbloy, I tried turning it on, and got this: (select max(enddate) as max, meterid from usermeasurements where locationid=$1 AND usage>-1 GROUP BY meterid ) as t1 left outer join usermeasurements as t2 ON(t1.meterid=t2.meterid AND t1.max=t2.enddate) Commented Jan 11, 2013 at 16:42
  • @IgorRomanchenko, well no, it is actually the whole query. I know that my style might not be the most beautiful, but its true :) If I C/P the query from the OP into pgAdmin, it works as it should Commented Jan 11, 2013 at 16:43

2 Answers 2

3

Since max is a keyword use something else after as

select distinct max(enddate) as mymax, ...... 

UPDATED :

Another thing. In your exception it says

unable to get usermeasurement:

but in your sql you have used usermeasurements (You have a s in the end). Check your source code again there may be you have missed letter s

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

3 Comments

Good catch, I think you can use AS "max" as well.
@guess_me I just checked, and the spelling is correct. I even tried C/P it from the code to phpPgAdmin and it worked flawlessly
@MortenSkov hmmm but I'm just wandering why it says error in without letter s
0

Maybe the query should look like:

SELECT * 
FROM
(SELECT DISTINCT max(enddate) as max, meterid 
 FROM usermeasurements 
 WHERE locationid = 127025 
   AND usage > -1 
 GROUP BY meterid ) as t1 
LEFT JOIN usermeasurements as t2 
     ON (t1.meterid=t2.meterid 
         AND t1.max=t2.enddate)

The query in the question isn't correct (it looks like a FROM part of the bigger query).

7 Comments

hmm....when I try to paste that into PgAdmin I get an similar error: ERROR: syntax error at or near "AS" LINE 9: ON (t1.meterid=t2.meterid) AS sub
@MortenSkov But there is no AS sub in the query. Where are you getting it from?
Maybe you have some query rewrite RULE on the tables? Or one of the tables is a VIEW ?
Naahh...It should all be pretty standard. I just installed the server before I started this project.
@MortenSkov The query in the question is syntactically incorrect. See the manual here.
|

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.