It it possible to get this result in one query?

From: Nick <nboutelier(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: It it possible to get this result in one query?
Date: 2010-10-14 23:49:07
Message-ID: 75dd976e-1825-4204-a634-49c5cb4fa06e@z30g2000prg.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is it possible to get the results of this snip of a function without
using a function? All tables include an id and title column.

tables := ARRAY[table_one,table_two,table_three]::VARCHAR;
CREATE TEMP TABLE final_results (id INTEGER, title VARCHAR, r_types
VARCHAR[]);
FOR t IN ARRAY_LOWER(tables,1) .. ARRAY_UPPER(tables,1) LOOP
FOR r IN EXECUTE 'SELECT id, title FROM ' || tables[t] LOOP
IF (SELECT TRUE FROM final_results WHERE id = r.id LIMIT 1) THEN
UPDATE final_results SET r_types =
array_append(r_types,tables[t]) WHERE id = r.id;
ELSE
INSERT INTO final_results (id,title,r_types) VALUES
(r.id,r.title,ARRAY[tables.t]);
END LOOP;
END LOOP;

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2010-10-15 00:06:11 Re: how to write an optimized sql with two same subsql?
Previous Message sunpeng 2010-10-14 23:34:10 how to write an optimized sql with two same subsql?