Thread: Doing multiple steps at once

Doing multiple steps at once

From
Detlef Jockheck
Date:
Hi,

Basis: I have two tables (a & b).
1st: I create/clear a temporary table c
2nd: doing a "insert into c select something from a"
3rd: doing a "insert into c select something from b"
4th: calculate a result with "select something from c group by column"
5th: go back to 1st after some checks/corrections in a.

Is it possible to do the four steps (1-4) at once (with a function, procedure
or so?)

ciao
Detlef

--
# Dipl. Ing. (FH) Detlef Jockheck
# E-mail: detlef@jockheck.de
# -------------------------------

Re: Doing multiple steps at once

From
Tom Lane
Date:
Detlef Jockheck <detlef@jockheck.de> writes:
> 1st: I create/clear a temporary table c
> 2nd: doing a "insert into c select something from a"
> 3rd: doing a "insert into c select something from b"
> 4th: calculate a result with "select something from c group by column"

> Is it possible to do the four steps (1-4) at once (with a function, procedure
> or so?)

Do you need a temp table at all?  The given calculation could be done
with something like

    SELECT whatever
    FROM (SELECT something FROM a
          UNION ALL
          SELECT something FROM b) ss
    GROUP BY column

I can't see a need for a temp table unless your intention is to scan the
UNION result multiple times, in which case building the temp table might
be faster than repeating the UNION.

            regards, tom lane