Re: Generating a SQL Server population routine - Mailing list pgsql-general

From Harald Fuchs
Subject Re: Generating a SQL Server population routine
Date
Msg-id puekxmvllp.fsf@srv.protecting.net
Whole thread Raw
In response to Generating a SQL Server population routine  (Martin_Hurst@dom.com)
List pgsql-general
In article <64591.66.212.203.144.1065659357.squirrel@$HOSTNAME>,
<btober@seaworthysys.com> writes:

> Or a little different, with the over-loaded functions relying on the
> original:

> CREATE OR REPLACE FUNCTION public.enum(int4) RETURNS  SETOF int4 AS '
>   DECLARE
>     numvals ALIAS FOR $1;
>   BEGIN
>     FOR currval IN 0 .. numvals - 1 LOOP
>       RETURN NEXT currval;
>     END LOOP;
>     RETURN;
>   END;
>   '  LANGUAGE 'plpgsql' VOLATILE;

Why VOLATILE?  Shouldn't that be IMMUTABLE?
(Sorry, but I'm a PostgreSQL newbie.)

> But, as interesting as these look, what would you actually use them for?

  SELECT extract (month FROM sdate) AS month,
         count (*) AS monthly_sales
  FROM sales
  GROUP BY month
  ORDER BY month;

gives you the monthly sales, but what if you would like a result row
also for months with nothing sold?

  SELECT enum, count (sdate) AS monthly_sales
  FROM enum (12, 1)
  LEFT JOIN sales ON enum = extract (month FROM sdate)
  GROUP BY enum
  ORDER BY enum;

This would do the trick.  Is there a more elegant solution?

pgsql-general by date:

Previous
From: Ang Chin Han
Date:
Subject: Re: Does postgresql support HKSCS ?
Next
From: Richard Huxton
Date:
Subject: Re: autoupdate sequences after copy