I'm new in postgresql. I'm trying to write a postgresql function for searching that will return matching IDs only and order IDs as column name supplied in the parameter.
I tried to solve the problem different ways.
I've read that order by in the conditional case don't order data if the ordering column don't have in the result set. But the function below I've written, returns IDs in ASC ordering iff the supplied column type in DB is text/character varying/date, I tested. For integer/float type column not sorting IDs at all. Now, I need to sort for integer/float type column and also ASC/DESC as needed.
CREATE OR REPLACE FUNCTION public.search_products(_name text DEFAULT NULL::text, _category_id integer DEFAULT NULL::integer, _min_mrp double precision DEFAULT NULL::double precision, _max_mrp double precision DEFAULT NULL::double precision, _sort_field text DEFAULT NULL::text)
RETURNS SETOF bigint
LANGUAGE sql
AS $function$
select product.id
from product
where
case
when _category_id is null then (_name is null or lower(product.name) like '%'|| lower(_name) ||'%' and (_min_mrp is null or _max_mrp is null Or (product.market_retail_price BETWEEN _min_mrp and _max_mrp)))
when _category_id is not null then (_name is null or lower(product.name) like '%'|| lower(_name) ||'%' and (_min_mrp is null or _max_mrp is null Or (product.market_retail_price BETWEEN _min_mrp and _max_mrp))) /*will be updated after category id deciding */
end
ORDER BY
CASE
WHEN(_sort_field similar to 'market_retail_price asc') THEN product.market_retail_price || ' ASC' /* it is float type column, and not working for asc/desc anything */
WHEN(_sort_field similar to 'approved_by asc') THEN product.approved_by || ' ASC' /* it is integer type column, and not working for asc/desc anything */
WHEN(_sort_field similar to 'approved_on asc') THEN product.approved_on || ' ASC' /* date type column, it works for asc order only. Even if text is 'approved_on desc' in other case, it match the case, but returns data in asc order only. THAT'S TOTALLY WIERED. Never sort desc. */
WHEN(_sort_field similar to 'supplier_id asc') THEN product.supplier_id || ' ASC'
WHEN(_sort_field similar to 'product_status asc') THEN product.product_status || ' ASC' /* text type, and working for asc only */
WHEN(_sort_field similar to 'name desc') THEN (product.name || ' DESC') /* not working for desc order, but returns data in asc sort */
ELSE product.id || ' ASC'
END
$function$
I also trying this way to order too simply: ORDERY BY _sort_field
and ORDER BY quote_ident(_sort_field);
it does not sort asc/desc for any column if the type text/date/integer/float whatever it is!
I'm using postgresql-9.5.1-1-windows-x64 version