Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Selecting count(*) from multiple tables - response (4) by dnoeth

$
0
0

You need to do the aggregates within Dervied Tables and then Outer join them:

SELECT
   acct.acc_no,
   acct.sort_code,
   COALESCE(credit.cnt, 0) AS credit_count,
   COALESCE(debit.cnt, 0) AS debit_count
FROM account_details acct
LEFT JOIN
 (
   SELECT acc_no, sort_code, COUNT(*) AS cnt
   FROM credit_trans
 ) AS credit
ON acct.acc_no = credit.acc_no
AND acct.sort_code = credit.sort_code
LEFT JOIN
 (
   SELECT acc_no, sort_code, COUNT(*) AS cnt
   FROM debit_trans
 ) AS debit
on acct.acc_no = debit.acc_no
AND acct.sort_code = debit.sort_code

Dieter


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>