sql - Query with CASE and generate_series(), order resulting timestamptz descending -


i'm new , trying create function following code:

create or replace function public.get_bulan() returns table (request_detail timestamp time zone) language plpgsql stable $function$         begin         return query          select         case         when (extract(day now()) >= 25) generate_series(date_trunc('year', now()), date_trunc('day', now()) ,interval '1 month')         when (select extract(month now()) = 2) now() - (interval '1' month * generate_series(0,1))         when (select extract(month now()) = 1) now() - (interval '1' month * generate_series(0,2))         else generate_series((select date(date_trunc('year', now()))), (select date(now())-'1 month'::interval), interval '1 month')         end         order timetstamptz(request_detail) desc;         end;         $function$; 

the result query above is:

2017-01-01 00:00:00 2017-02-01 00:00:00 2017-03-01 00:00:00 2017-04-01 00:00:00 2017-05-01 00:00:00 2017-06-01 00:00:00 2017-07-01 00:00:00 

i tried using order desc , order timestamp desc doesn't work. want make order descending result 2017-07-01 2017-01-01. how do that?

you have 2 ways that:

  1. add as result_timestamp order result_timestamp desc;

    create or replace function public.get_bulan()  returns table (request_detail timestamp time zone)  language plpgsql stable  $function$     begin     return query      select      case     when (extract(day now()) >= 25) generate_series(date_trunc('year', now()), date_trunc('day', now()) ,interval '1 month')     when (select extract(month now()) = 2) now() - (interval '1' month * generate_series(0,1))     when (select extract(month now()) = 1) now() - (interval '1' month * generate_series(0,2))     else generate_series((select date(date_trunc('year', now()))), (select date(now())-'1 month'::interval), interval '1 month')     end result_timestamp order result_timestamp desc;      end;     $function$; 
  2. alternatively can delete ordering procedure , order when u call other place:

    create or replace function public.get_bulan() returns table (request_detail timestamp time zone) language plpgsql stable $function$         begin         return query          select          case         when (extract(day now()) >= 25) generate_series(date_trunc('year', now()), date_trunc('day', now()) ,interval '1 month')         when (select extract(month now()) = 2) now() - (interval '1' month * generate_series(0,1))         when (select extract(month now()) = 1) now() - (interval '1' month * generate_series(0,2))         else generate_series((select date(date_trunc('year', now()))), (select date(now())-'1 month'::interval), interval '1 month')         end;          end;         $function$; 

and calling:

select request_detail public.get_bulan() order request_detail desc 

note: second 1 pliable , can manipulate.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -