Re: How to compare the results of two queries?

From: Kevin Grittner <kgrittn(at)ymail(dot)com>
To: Juan Daniel Santana Rodés <jdsantana(at)estudiantes(dot)uci(dot)cu>, Igor Neyman <ineyman(at)perceptron(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to compare the results of two queries?
Date: 2013-09-17 18:28:45
Message-ID: 1379442525.78757.YahooMailNeo@web162904.mail.bf1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Juan Daniel Santana Rodés <jdsantana(at)estudiantes(dot)uci(dot)cu> wrote:

> I am developing a task in which I need to know how to compare the
> results of two queries ...
> I thought about creating a procedure which both queries received by
> parameters respectively. Then somehow able to run queries and return if
> both have the same result. As a feature of the problem, both queries are
> selection.

Maybe something roughly like this?:

create or replace function rscmp(qry1 text, qry2 text)
  returns boolean
  language plpgsql
as $$
declare
  c int;
begin
  execute 'select count(*) from ('
       || qry1
       || ') rs1 full join ('
       || qry2
       || ') rs2 on rs1 = rs2 where rs1 is not distinct from null or rs2 is not distinct from null'
    into c;
  return (c = 0);
exception
  when sqlstate '42804' then return false;
end;
$$;

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2013-09-17 18:30:19 Re: Connect postgres to SQLSERVER
Previous Message Jeff Janes 2013-09-17 18:28:14 Re: upgrade from 9.2.x to 9.3 causes significant performance degradation