Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

The below postgresql query works fine if directly run on server through PuTTY. But throws error "Syntax error at the end of input" if run through java program. The postgresql version is 9.1.9 and uses postgresql-9.4-1201.jdbc4.jar.

I tried replacing 'TN' with $$TN$$ as suggested by another thread, but not helpful. What is going wrong here?

     WITH cte AS (    
      SELECT sum(((plin_sale_price*plin_quantity)-plin_coupon_amt)-
          (ss_wholesale_cost*plin_quantity)) AS sum_profit  
         , sum(plin_sale_price*plin_quantity) AS sum_price  
         , i_category
         , i_class  
      from  
         mater_purchase2 mp, mater_purchase_lineitem2 mpl  
         ,date_dim       d1  
         ,item  
         ,store  
      where  
         d1.d_year = 2001   
         and mp.purc_purchase_id = mpl.plin_purchase_id  
         and d1.d_date_sk = mp.ss_sold_date_sk  
         and i_item_sk  = mpl.ss_item_sk   
         and s_store_sk  = mp.ss_store_sk  
         and s_state in ('TN')  
         GROUP  BY i_category, i_class  
      )  
      SELECT sum(sum_profit)/sum(sum_price) AS gross_margin  
         , NULL AS i_category, NULL AS i_class,2 as locheirarchy 
         , 1 AS rank_within_parent  
      FROM   cte  

   UNION ALL  
   (
      SELECT sum(sum_profit)/sum(sum_price) AS gross_margin  
         , i_category, NULL AS i_class, 1 as locheirarchy  
         , rank() OVER (ORDER BY sum(sum_profit)/sum(sum_price)) AS rank_within_parent  
    FROM   cte  
    GROUP  BY i_category  
   )  

   UNION ALL  
   (
      SELECT sum_profit/sum_price AS gross_margin  
         , i_category, i_class, 0 as locheirarchy  
         , rank() OVER (PARTITION BY i_category  
                ORDER BY sum_profit/sum_price) AS rank_within_parent  
      FROM   cte  
   )  
   limit 100;  
share|improve this question

put on hold as off-topic by dezso, Paul White, Colin 't Hart, Erwin Brandstetter, RolandoMySQLDBA Mar 12 at 15:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – dezso, Paul White, Colin 't Hart, Erwin Brandstetter, RolandoMySQLDBA
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please use the provided formatting aids to format your code block and provide the version of Postgres and JDBC. Also, you cannot run queries "directly on the server". Which client did you use? –  Erwin Brandstetter Mar 12 at 0:33
2  
The error is resolved. I tried running it step by step and the error was coming due to some commented lines present in between the query!!Once I tried after removing these lines, query worked perfectly fine. –  vkb Mar 12 at 10:08

Browse other questions tagged or ask your own question.