ERROR: for SELECT DISTINCT, ORDER BY expressions should appear in the select list

I have a table "campaign_items" with columns (budget spent), and I want to calculate the remaining budget using the formula remaining budget = budget - spent

Now I run a query:

select distinct a.budget,a.spent 
from campaign_items a 
where campaign_item_id=12345 
order by a.budget-a.spent

But I get an error message:

ERROR: for SELECT DISTINCT, ORDER BY expressions should appear in the select list

Note. I cannot remove the keyword DISTINCTfrom the request because the request is created using JdbcTemplate

Can someone help me deal with this error?

+4
source share
1 answer

, , ORDER BY a.budget - a.spent, SELECT. , , budget spent.

select t.budget, t.spent
from
(
    select distinct a.budget,
                    a.spent,
                    a.budget - a.spent as sort,
    from campaign_items a
    where campaign_item_id = 12345
) t
order by t.sort
+5

Source: https://habr.com/ru/post/1666706/


All Articles