Re: [SQL] Single SQL to find if any record exists across several tables - Mailing list pgsql-sql

From David G. Johnston
Subject Re: [SQL] Single SQL to find if any record exists across several tables
Date
Msg-id CAKFQuwbVLvU_wPCtQ35+2DyJjMrLmzrMKRuQF4JfRsKQkC8sTw@mail.gmail.com
Whole thread Raw
In response to [SQL] Single SQL to find if any record exists across several tables  (Ertan Küçükoğlu <ertan.kucukoglu@1nar.com.tr>)
List pgsql-sql
On Thu, Sep 21, 2017 at 5:45 AM, Ertan Küçükoğlu <ertan.kucukoglu@1nar.com.tr> wrote:

Is it possible to build a single SQL statement and include all tables in it
so that I receive single "count(fk)" sum across all tables?

Several sample SQLs run at the moment are as follows:
Select count(fk) from cari
Select count(fk) from sipbaslik
Select count(fk) from stkdepo
Select count(fk) from stkkart
Select count(fk) from stkhar

​Without delving into dynamic SQL you can do either:

SELECT sum(count) FROM
(
SELECT count(fk) FROM can
UNION ALL
SELECT count(fk) FROM sipbaslik
)​ src

OR

SELECT 
(SELECT count(fk) FROM can)
+
(SELECT count(fk) FROM sipbaslik)

David J.

pgsql-sql by date:

Previous
From: Ertan Küçükoğlu
Date:
Subject: [SQL] Single SQL to find if any record exists across several tables
Next
From: "Vishwanath M.C"
Date:
Subject: [SQL] Not able to find the postgres version in stack builder