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;